Skip to content

Commit c0f3b2d

Browse files
committed
2024 day 25
1 parent 0e4cbd4 commit c0f3b2d

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#####
2+
.####
3+
.####
4+
.####
5+
.#.#.
6+
.#...
7+
.....
8+
9+
#####
10+
##.##
11+
.#.##
12+
...##
13+
...#.
14+
...#.
15+
.....
16+
17+
.....
18+
#....
19+
#....
20+
#...#
21+
#.#.#
22+
#.###
23+
#####
24+
25+
.....
26+
.....
27+
#.#..
28+
###..
29+
###.#
30+
###.#
31+
#####
32+
33+
.....
34+
.....
35+
.....
36+
#....
37+
#.#..
38+
#.#.#
39+
#####

crates/year2024/src/day25.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use utils::prelude::*;
2+
3+
/// Finding matching keys and locks.
4+
#[derive(Clone, Debug)]
5+
pub struct Day25 {
6+
locks: Vec<u32>,
7+
keys: Vec<u32>,
8+
}
9+
10+
impl Day25 {
11+
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
12+
let mut locks = Vec::new();
13+
let mut keys = Vec::new();
14+
15+
for item in parser::one_of((b'.'.map(|_| false), b'#'.map(|_| true)))
16+
.repeat_n::<5, _>(parser::noop())
17+
.repeat_n::<7, _>(parser::eol())
18+
.with_suffix(parser::eol().then(parser::eol()))
19+
.parse_iterator(input)
20+
{
21+
let grid = item?;
22+
23+
let top = grid[0][0];
24+
let bottom = grid[6][0];
25+
if top == bottom
26+
|| grid[0][1..].iter().any(|&x| x != top)
27+
|| grid[6][1..].iter().any(|&x| x != bottom)
28+
{
29+
return Err(InputError::new(input, 0, "expected lock or key"));
30+
}
31+
32+
let mut mask = 0u32;
33+
for c in 0..5 {
34+
let mut h = 0;
35+
for row in grid[1..].iter() {
36+
if row[c] != top {
37+
break;
38+
}
39+
h += 1;
40+
}
41+
mask |= ((1 << h) - 1) << (c * 5);
42+
}
43+
44+
if top {
45+
locks.push(mask);
46+
} else {
47+
keys.push(mask);
48+
}
49+
}
50+
51+
Ok(Self { locks, keys })
52+
}
53+
54+
#[must_use]
55+
pub fn part1(&self) -> u32 {
56+
let mut total = 0;
57+
for &lock in &self.locks {
58+
for &key in &self.keys {
59+
if lock & key == lock {
60+
total += 1;
61+
}
62+
}
63+
}
64+
total
65+
}
66+
67+
#[must_use]
68+
pub fn part2(&self) -> &'static str {
69+
"🎄"
70+
}
71+
}
72+
73+
examples!(Day25 -> (u32, &'static str) [
74+
{file: "day25_example0.txt", part1: 3},
75+
]);

crates/year2024/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ utils::year!(2024 => year2024, ${
2626
22 => day22::Day22,
2727
23 => day23::Day23,
2828
24 => day24::Day24,
29+
25 => day25::Day25,
2930
});

0 commit comments

Comments
 (0)