Skip to content

Commit

Permalink
day 4 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
asmello committed Dec 4, 2023
1 parent 7baa4dc commit 3d56a76
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
36 changes: 33 additions & 3 deletions src/day4.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use chumsky::{
error::Simple,
primitive::{choice, just},
primitive::just,
text::{self, TextParser},
Parser,
};
use eyre::{eyre, Context};
use std::{
collections::HashSet,
collections::{HashMap, HashSet},
io::{BufRead, BufReader, Read},
str::FromStr,
};
Expand All @@ -24,6 +24,33 @@ pub fn part1(input: impl Read) -> eyre::Result<usize> {
Ok(sum)
}

pub fn part2(input: impl Read) -> eyre::Result<usize> {
let buf_reader = BufReader::new(input);
let mut counts: HashMap<_, usize> = HashMap::new();
for (num, line) in buf_reader.lines().enumerate() {
let line = line.wrap_err_with(|| format!("could not read line {num}"))?;
let card: Card = line
.parse()
.wrap_err_with(|| format!("could not parse card at line {num}"))?;
// the count of copies won by past cards
let count = counts.entry(card.id).or_default();
*count += 1; // count the original card
let count = *count; // drop the borrow
let matches = card.matches();
for id in card.id + 1..=card.id + matches {
*counts.entry(id).or_default() += count;
}
// println!(
// "card: {} has {}, matched {} -- set is {:?}",
// card.id,
// count,
// card.matches(),
// counts
// );
}
Ok(counts.into_values().sum())
}

#[derive(Default, Debug)]
struct Card {
id: usize,
Expand All @@ -33,13 +60,16 @@ struct Card {

impl Card {
fn points(&self) -> usize {
let matches = self.winning.intersection(&self.scratched).count();
let matches = self.matches();
if matches == 0 {
0
} else {
2_usize.pow((matches - 1) as u32)
}
}
fn matches(&self) -> usize {
self.winning.intersection(&self.scratched).count()
}
}

impl FromStr for Card {
Expand Down
16 changes: 15 additions & 1 deletion tests/day4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const SAMPLE: &str = indoc! { r#"
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 1
"# };
const INPUT_PATH: &str = "day4/input.txt";

#[test]
fn part1_sample() {
Expand All @@ -20,7 +21,20 @@ fn part1_sample() {

#[test]
fn part1_input() {
let input = common::read("day4/input.txt").unwrap();
let input = common::read(INPUT_PATH).unwrap();
let result = day4::part1(input).unwrap();
assert_eq!(result, 25004);
}

#[test]
fn part2_sample() {
let result = day4::part2(SAMPLE.as_bytes()).unwrap();
assert_eq!(result, 30);
}

#[test]
fn part2_input() {
let input = common::read(INPUT_PATH).unwrap();
let result = day4::part2(input).unwrap();
assert_eq!(result, 14427616);
}

0 comments on commit 3d56a76

Please sign in to comment.