-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.rs
78 lines (69 loc) · 1.91 KB
/
day22.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
use std::iter;
use std::sync::atomic::{AtomicU32, Ordering};
use itertools::Itertools;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
fn step(num: u32) -> u32 {
let num = (num ^ num << 6) & 16777215;
let num = (num ^ num >> 5) & 16777215;
(num ^ num << 11) & 16777215
}
pub fn part1(data: &str) -> u64 {
data.lines()
.collect::<Vec<_>>()
.par_iter()
.filter_map(|line| iter::successors(line.parse().ok(), |num| Some(step(*num))).nth(2000))
.map(Into::<u64>::into)
.sum()
}
pub fn part2(data: &str) -> u32 {
let results = &[const { AtomicU32::new(0) }; 19 * 19 * 19 * 19];
data.lines()
.collect::<Vec<_>>()
.par_iter()
.filter_map(|line| {
let mut seen = [false; 19 * 19 * 19 * 19];
iter::successors(line.parse().ok(), |num| Some(step(*num)))
.take(2001)
.map(|num| num % 10)
.tuple_windows()
.filter_map(|(a, b, c, d, e)| {
let key = ((((9 + a - b) * 19 + 9 + b - c) * 19 + 9 + c - d) * 19 + 9 + d - e)
as usize;
if !seen[key] {
seen[key] = true;
Some(results[key].fetch_add(e, Ordering::AcqRel) + e)
} else {
None
}
})
.max()
})
.max()
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
static EXAMPLE_1: &str = indoc! {"
1
10
100
2024
"};
static EXAMPLE_2: &str = indoc! {"
1
2
3
2024
"};
#[test]
fn part1_examples() {
assert_eq!(37327623, part1(EXAMPLE_1));
}
#[test]
fn part2_examples() {
assert_eq!(23, part2(EXAMPLE_2));
}
}