-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_22.rs
106 lines (86 loc) · 2.19 KB
/
day_22.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::iter::{once, repeat};
use common::{solution, Answer};
use itertools::Itertools;
use rayon::iter::{ParallelBridge, ParallelIterator};
solution!("Monkey Market", 22);
fn part_a(input: &str) -> Answer {
let mut sum = 0;
for num in input.lines() {
let mut num = num.parse::<u64>().unwrap();
(0..2000).for_each(|_| {
next(&mut num);
});
sum += num;
}
sum.into()
}
fn part_b(input: &str) -> Answer {
let mut buyers = Vec::new();
for num in input.lines() {
let mut num = num.parse::<u64>().unwrap();
let seq = once(num)
.chain((0..2000).map(|_| next(&mut num)))
.collect::<Vec<_>>();
buyers.push(seq);
}
let diffs = buyers
.iter()
.map(|buyer| {
buyer
.iter()
.tuple_windows()
.map(|(&a, &b)| (b % 10) as i8 - (a % 10) as i8)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let out = repeat(-9..=9)
.take(4)
.multi_cartesian_product()
.map(|x| (x[0], x[1], x[2], x[3]))
.par_bridge()
.map(|(a, b, c, d)| {
let mut sum = 0;
for (diff, nums) in diffs.iter().zip(buyers.iter()) {
if let Some(idx) = find_sequence(diff, (a, b, c, d)) {
sum += nums[idx + 4] % 10;
}
}
sum
})
.max()
.unwrap();
out.into()
}
fn next(num: &mut u64) -> u64 {
*num ^= *num * 64;
*num %= 16777216;
*num ^= *num / 32;
*num %= 16777216;
*num ^= *num * 2048;
*num %= 16777216;
*num
}
fn find_sequence(haystack: &[i8], (a, b, c, d): (i8, i8, i8, i8)) -> Option<usize> {
haystack
.iter()
.tuple_windows()
.position(|(&ax, &bx, &cx, &dx)| ax == a && bx == b && cx == c && dx == d)
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
1
10
100
2024
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 37327623.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 24.into());
}
}