aoc/year2024/day25.rs
1//! # Code Chronicle
2//!
3//! Efficiently checks if locks and keys overlap using bitwise logic. The ASCII character
4//! `#` (35) is odd and `.` (46) is even so bitwise AND with 1 results in either 1 or 0.
5//! The newline character `\n` (10) is even so will result in 0 and not contribute to matches.
6//! There are 25 bits plus 4 newline bits so each lock or key can be stored in a `u32`.
7//! For example:
8//!
9//! ```none
10//! #####
11//! ##.## 11011
12//! .#.## 01011
13//! ...## => 00011 => 110110_010110_000110_000100_00010
14//! ...#. 00010
15//! ...#. 00010
16//! .....
17//! ```
18pub fn parse(input: &str) -> &str {
19 input
20}
21
22pub fn part1(input: &str) -> u32 {
23 let mut locks = Vec::with_capacity(250);
24 let mut keys = Vec::with_capacity(250);
25
26 for slice in input.as_bytes().chunks(43) {
27 let bits = slice[6..35].iter().fold(0, |bits, &n| (bits << 1) | (n & 1) as u32);
28
29 if slice[0] == b'#' {
30 locks.push(bits);
31 } else {
32 keys.push(bits);
33 }
34 }
35
36 locks.iter().map(|lock| keys.iter().map(|key| u32::from(lock & key == 0)).sum::<u32>()).sum()
37}
38
39pub fn part2(_input: &str) -> &'static str {
40 "n/a"
41}