Skip to content

Commit

Permalink
Optimize 2024 day 25 parsing
Browse files Browse the repository at this point in the history
The order of the bits doesn't matter
  • Loading branch information
ictrobot committed Dec 25, 2024
1 parent 397ac5c commit 824d343
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions crates/year2024/src/day25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,11 @@ impl Day25 {
return Err(InputError::new(input, grid_str, "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);
}
let mask = grid[1..6]
.as_flattened()
.iter()
.enumerate()
.fold(0, |acc, (i, &x)| acc | (u32::from(x == top) << i));

if top {
locks.push(mask);
Expand Down

0 comments on commit 824d343

Please sign in to comment.