Skip to content

Commit a82d093

Browse files
committed
[2024] Slightly cleanup day 22
1 parent 801e25b commit a82d093

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

aoc_2024/src/day_22.rs

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::iter::repeat;
1+
use std::iter::{once, repeat};
22

33
use common::{solution, Answer};
44
use itertools::Itertools;
@@ -11,18 +11,9 @@ fn part_a(input: &str) -> Answer {
1111

1212
for num in input.lines() {
1313
let mut num = num.parse::<u64>().unwrap();
14-
15-
for _ in 0..2000 {
16-
num ^= num * 64;
17-
num %= 16777216;
18-
19-
num ^= num / 32;
20-
num %= 16777216;
21-
22-
num ^= num * 2048;
23-
num %= 16777216;
24-
}
25-
14+
(0..2000).for_each(|_| {
15+
next(&mut num);
16+
});
2617
sum += num;
2718
}
2819

@@ -31,51 +22,35 @@ fn part_a(input: &str) -> Answer {
3122

3223
fn part_b(input: &str) -> Answer {
3324
let mut buyers = Vec::new();
34-
let mut diffs = Vec::new();
35-
3625
for num in input.lines() {
3726
let mut num = num.parse::<u64>().unwrap();
38-
let mut seq = vec![num];
39-
40-
for _ in 0..2000 {
41-
num ^= num * 64;
42-
num %= 16777216;
43-
44-
num ^= num / 32;
45-
num %= 16777216;
46-
47-
num ^= num * 2048;
48-
num %= 16777216;
49-
50-
seq.push(num);
51-
}
52-
27+
let seq = once(num)
28+
.chain((0..2000).map(|_| next(&mut num)))
29+
.collect::<Vec<_>>();
5330
buyers.push(seq);
5431
}
5532

56-
for buyer in buyers.iter() {
57-
let mut diff = Vec::new();
58-
for (&a, &b) in buyer.iter().tuple_windows() {
59-
diff.push((b % 10) as i8 - (a % 10) as i8);
60-
}
61-
diffs.push(diff);
62-
}
33+
let diffs = buyers
34+
.iter()
35+
.map(|buyer| {
36+
buyer
37+
.iter()
38+
.tuple_windows()
39+
.map(|(&a, &b)| (b % 10) as i8 - (a % 10) as i8)
40+
.collect::<Vec<_>>()
41+
})
42+
.collect::<Vec<_>>();
6343

6444
let out = repeat(-9..=9)
6545
.take(4)
6646
.multi_cartesian_product()
47+
.map(|x| (x[0], x[1], x[2], x[3]))
6748
.par_bridge()
68-
.map(|x| {
69-
let (a, b, c, d) = (x[0], x[1], x[2], x[3]);
49+
.map(|(a, b, c, d)| {
7050
let mut sum = 0;
7151

7252
for (diff, nums) in diffs.iter().zip(buyers.iter()) {
73-
let idx = diff
74-
.iter()
75-
.tuple_windows()
76-
.position(|(&ax, &bx, &cx, &dx)| ax == a && bx == b && cx == c && dx == d);
77-
78-
if let Some(idx) = idx {
53+
if let Some(idx) = find_sequence(diff, (a, b, c, d)) {
7954
sum += nums[idx + 4] % 10;
8055
}
8156
}
@@ -88,6 +63,26 @@ fn part_b(input: &str) -> Answer {
8863
out.into()
8964
}
9065

66+
fn next(num: &mut u64) -> u64 {
67+
*num ^= *num * 64;
68+
*num %= 16777216;
69+
70+
*num ^= *num / 32;
71+
*num %= 16777216;
72+
73+
*num ^= *num * 2048;
74+
*num %= 16777216;
75+
76+
*num
77+
}
78+
79+
fn find_sequence(haystack: &[i8], (a, b, c, d): (i8, i8, i8, i8)) -> Option<usize> {
80+
haystack
81+
.iter()
82+
.tuple_windows()
83+
.position(|(&ax, &bx, &cx, &dx)| ax == a && bx == b && cx == c && dx == d)
84+
}
85+
9186
#[cfg(test)]
9287
mod test {
9388
use indoc::indoc;

0 commit comments

Comments
 (0)