Skip to content

Commit 824d343

Browse files
committed
Optimize 2024 day 25 parsing
The order of the bits doesn't matter
1 parent 397ac5c commit 824d343

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

crates/year2024/src/day25.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,11 @@ impl Day25 {
3030
return Err(InputError::new(input, grid_str, "expected lock or key"));
3131
}
3232

33-
let mut mask = 0u32;
34-
for c in 0..5 {
35-
let mut h = 0;
36-
for row in grid[1..].iter() {
37-
if row[c] != top {
38-
break;
39-
}
40-
h += 1;
41-
}
42-
mask |= ((1 << h) - 1) << (c * 5);
43-
}
33+
let mask = grid[1..6]
34+
.as_flattened()
35+
.iter()
36+
.enumerate()
37+
.fold(0, |acc, (i, &x)| acc | (u32::from(x == top) << i));
4438

4539
if top {
4640
locks.push(mask);

0 commit comments

Comments
 (0)