diff --git a/crates/year2024/examples/day25_example0.txt b/crates/year2024/examples/day25_example0.txt new file mode 100644 index 0000000..a53fe9b --- /dev/null +++ b/crates/year2024/examples/day25_example0.txt @@ -0,0 +1,39 @@ +##### +.#### +.#### +.#### +.#.#. +.#... +..... + +##### +##.## +.#.## +...## +...#. +...#. +..... + +..... +#.... +#.... +#...# +#.#.# +#.### +##### + +..... +..... +#.#.. +###.. +###.# +###.# +##### + +..... +..... +..... +#.... +#.#.. +#.#.# +##### \ No newline at end of file diff --git a/crates/year2024/src/day25.rs b/crates/year2024/src/day25.rs new file mode 100644 index 0000000..570a645 --- /dev/null +++ b/crates/year2024/src/day25.rs @@ -0,0 +1,75 @@ +use utils::prelude::*; + +/// Finding matching keys and locks. +#[derive(Clone, Debug)] +pub struct Day25 { + locks: Vec, + keys: Vec, +} + +impl Day25 { + pub fn new(input: &str, _: InputType) -> Result { + let mut locks = Vec::new(); + let mut keys = Vec::new(); + + for item in parser::one_of((b'.'.map(|_| false), b'#'.map(|_| true))) + .repeat_n::<5, _>(parser::noop()) + .repeat_n::<7, _>(parser::eol()) + .with_suffix(parser::eol().then(parser::eol())) + .parse_iterator(input) + { + let grid = item?; + + let top = grid[0][0]; + let bottom = grid[6][0]; + if top == bottom + || grid[0][1..].iter().any(|&x| x != top) + || grid[6][1..].iter().any(|&x| x != bottom) + { + return Err(InputError::new(input, 0, "expected lock or key")); + } + + let mut mask = 0u32; + for c in 0..5 { + let mut h = 0; + for row in grid[1..].iter() { + if row[c] != top { + break; + } + h += 1; + } + mask |= ((1 << h) - 1) << (c * 5); + } + + if top { + locks.push(mask); + } else { + keys.push(mask); + } + } + + Ok(Self { locks, keys }) + } + + #[must_use] + pub fn part1(&self) -> u32 { + let mut total = 0; + for &lock in &self.locks { + for &key in &self.keys { + if lock & key == lock { + total += 1; + } + } + } + total + } + + #[must_use] + pub fn part2(&self) -> &'static str { + "🎄" + } +} + +examples!(Day25 -> (u32, &'static str) [ + {file: "day25_example0.txt", part1: 3}, +]); diff --git a/crates/year2024/src/lib.rs b/crates/year2024/src/lib.rs index ed5fbce..b76eec5 100644 --- a/crates/year2024/src/lib.rs +++ b/crates/year2024/src/lib.rs @@ -26,4 +26,5 @@ utils::year!(2024 => year2024, ${ 22 => day22::Day22, 23 => day23::Day23, 24 => day24::Day24, + 25 => day25::Day25, });