diff --git a/data/examples/04.txt b/data/examples/04.txt new file mode 100644 index 000000000..9bdb874 --- /dev/null +++ b/data/examples/04.txt @@ -0,0 +1,6 @@ +Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 +Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 +Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 +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 11 diff --git a/src/bin/04.rs b/src/bin/04.rs new file mode 100644 index 000000000..3f13d20 --- /dev/null +++ b/src/bin/04.rs @@ -0,0 +1,79 @@ +use std::str::FromStr; + +use aoc::parsers::to_vec; + +struct ParseCardError; + +struct Card { + winning: Vec, + numbers: Vec, +} + +impl FromStr for Card { + type Err = ParseCardError; + fn from_str(s: &str) -> Result { + let (_, rest) = s.split_once(": ").unwrap(); + let (win, my) = rest.split_once(" | ").unwrap(); + + let winning: Vec = to_vec(win, ' '); + let numbers: Vec = to_vec(my, ' '); + + Ok(Card { winning, numbers }) + } +} + +impl Card { + fn n_matches(&self) -> usize { + self.numbers + .iter() + .filter(|n| self.winning.contains(n)) + .count() + } + fn score(&self) -> u32 { + let c = self.n_matches(); + + if c == 0 { + return 0; + } + + 2_u32.pow((self.n_matches() - 1) as u32) + } +} + +pub fn part_one(input: &str) -> Option { + Some( + input + .lines() + .filter_map(|l| l.parse::().ok()) + .map(|g| g.score()) + .sum(), + ) +} + +pub fn part_two(input: &str) -> Option { + let cards: Vec = to_vec(input, '\n'); + let mut multiples = vec![1; cards.len()]; + + for (i, card) in cards.iter().enumerate() { + for j in 0..card.n_matches() { + multiples[i + j + 1] += multiples[i]; + } + } + + Some(multiples.iter().sum::()) +} + +aoc::solution!(4); + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_part_one() { + assert_eq!(part_one(&aoc::template::read_file("examples", 4)), Some(13)); + } + #[test] + fn test_part_two() { + assert_eq!(part_two(&aoc::template::read_file("examples", 4)), Some(30)); + } +}