Skip to content

Commit 343f95b

Browse files
committed
Tidy
1 parent 1a584cd commit 343f95b

File tree

5 files changed

+45
-77
lines changed

5 files changed

+45
-77
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
9797
| 21 | [Keypad Conundrum](https://adventofcode.com/2024/day/21) | [Source](src/year2024/day21.rs) | 111 |
9898
| 22 | [Monkey Market](https://adventofcode.com/2024/day/22) | [Source](src/year2024/day22.rs) | 1350 |
9999
| 23 | [LAN Party](https://adventofcode.com/2024/day/23) | [Source](src/year2024/day23.rs) | 43 |
100-
| 24 | [Crossed Wires](https://adventofcode.com/2024/day/24) | [Source](src/year2024/day24.rs) | 36 |
101-
| 25 | [Code Chronicle](https://adventofcode.com/2024/day/25) | [Source](src/year2024/day25.rs) | 12 |
100+
| 24 | [Crossed Wires](https://adventofcode.com/2024/day/24) | [Source](src/year2024/day24.rs) | 23 |
101+
| 25 | [Code Chronicle](https://adventofcode.com/2024/day/25) | [Source](src/year2024/day25.rs) | 11 |
102102

103103
## 2023
104104

docs/pie-2024.svg

Lines changed: 3 additions & 3 deletions
Loading

docs/pie-all.svg

Lines changed: 3 additions & 3 deletions
Loading

src/year2024/day24.rs

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use crate::util::hash::*;
33
use crate::util::iter::*;
44
use crate::util::parse::*;
5+
use std::collections::VecDeque;
56

67
type Input<'a> = (&'a str, Vec<[&'a str; 5]>);
78

@@ -14,39 +15,40 @@ pub fn parse(input: &str) -> Input<'_> {
1415
pub fn part1(input: &Input<'_>) -> u64 {
1516
let (prefix, gates) = input;
1617

17-
let mut names = FastMap::new();
18-
let mut cache = FastMap::new();
19-
let mut ops = FastMap::new();
18+
let mut todo: VecDeque<_> = gates.iter().copied().collect();
19+
let mut cache = vec![u8::MAX; 1 << 15];
20+
let mut result = 0;
21+
22+
let to_index = |s: &str| {
23+
let b = s.as_bytes();
24+
((b[0] as usize & 31) << 10) + ((b[1] as usize & 31) << 5) + (b[2] as usize & 31)
25+
};
2026

2127
for line in prefix.lines() {
2228
let prefix = &line[..3];
2329
let suffix = &line[5..];
24-
25-
let size = names.len();
26-
let index = *names.entry(prefix).or_insert(size);
27-
28-
cache.insert(index, suffix.unsigned());
30+
cache[to_index(prefix)] = suffix.unsigned();
2931
}
3032

31-
for &[left, kind, right, _, to] in gates {
32-
let size = names.len();
33-
let left = *names.entry(left).or_insert(size);
34-
35-
let size = names.len();
36-
let right = *names.entry(right).or_insert(size);
37-
38-
let size = names.len();
39-
let to = *names.entry(to).or_insert(size);
40-
41-
ops.insert(to, (left, kind, right));
33+
while let Some(gate @ [left, kind, right, _, to]) = todo.pop_front() {
34+
let left = cache[to_index(left)];
35+
let right = cache[to_index(right)];
36+
37+
if left == u8::MAX || right == u8::MAX {
38+
todo.push_back(gate);
39+
} else {
40+
cache[to_index(to)] = match kind {
41+
"AND" => left & right,
42+
"OR" => left | right,
43+
"XOR" => left ^ right,
44+
_ => unreachable!(),
45+
}
46+
}
4247
}
4348

44-
let mut result = 0;
45-
46-
for i in (0..64).rev() {
47-
let key = format!("z{i:02}");
48-
if let Some(&key) = names.get(key.as_str()) {
49-
result = (result << 1) | helper(&mut cache, &mut ops, key);
49+
for i in (to_index("z00")..to_index("z64")).rev() {
50+
if cache[i] != u8::MAX {
51+
result = (result << 1) | (cache[i] as u64);
5052
}
5153
}
5254

@@ -102,27 +104,3 @@ pub fn part2(input: &Input<'_>) -> String {
102104
result.sort_unstable();
103105
result.join(",")
104106
}
105-
106-
fn helper(
107-
cache: &mut FastMap<usize, u64>,
108-
ops: &mut FastMap<usize, (usize, &str, usize)>,
109-
key: usize,
110-
) -> u64 {
111-
if let Some(&value) = cache.get(&key) {
112-
return value;
113-
}
114-
115-
let (left, op, right) = ops[&key];
116-
let left = helper(cache, ops, left);
117-
let right = helper(cache, ops, right);
118-
119-
let value = match op {
120-
"AND" => left & right,
121-
"OR" => left | right,
122-
"XOR" => left ^ right,
123-
_ => unreachable!(),
124-
};
125-
126-
cache.insert(key, value);
127-
value
128-
}

src/year2024/day25.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
//! # Code Chronicle
2+
const MASK: u64 = 0b_011111_011111_011111_011111_011111;
3+
24
pub fn parse(input: &str) -> &str {
35
input
46
}
57

68
pub fn part1(input: &str) -> u32 {
7-
let mut iter = input.bytes();
9+
let mut slice = input.as_bytes();
810
let mut locks = Vec::with_capacity(250);
911
let mut keys = Vec::with_capacity(250);
1012
let mut result = 0;
1113

12-
loop {
13-
let mut bits = 0;
14-
15-
for _ in 0..41 {
16-
let b = iter.next().unwrap();
17-
bits = (bits << 1) | (b & 1) as u64;
18-
}
14+
while !slice.is_empty() {
15+
let bits = slice[6..35].iter().fold(0, |bits, &n| (bits << 1) | (n & 1) as u64);
1916

20-
bits &= 0b011111_011111_011111_011111_011111_011111_011111;
21-
22-
if bits & 0b011111_000000_000000_000000_000000_000000_000000 != 0 {
23-
locks.push(bits);
17+
if slice[0] == b'#' {
18+
locks.push(bits & MASK);
2419
} else {
25-
keys.push(bits);
20+
keys.push(bits & MASK);
2621
}
2722

28-
iter.next();
29-
if iter.next().is_none() {
30-
break;
31-
}
23+
slice = &slice[43.min(slice.len())..];
3224
}
3325

3426
for lock in &locks {
3527
for key in &keys {
36-
if lock & key == 0 {
37-
result += 1;
38-
}
28+
result += (lock & key == 0) as u32;
3929
}
4030
}
4131

0 commit comments

Comments
 (0)