-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2024_22.rs
135 lines (104 loc) · 3.09 KB
/
solution_2024_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::collections::HashMap;
use advent_of_code_common::parsing::{Error, parse_lines_to_vec};
const DATA: &str = include_str!("../../resources/22.txt");
type N = u64;
type Diff = i8;
type Input = Vec<N>;
const SEQUENCE_LENGTH: usize = 4;
type Sequence = [Diff; SEQUENCE_LENGTH];
fn parse(input: &str) -> Result<Input, Error> {
parse_lines_to_vec(input)
}
fn next(n: N) -> N {
fn mix_prune(a: N, b: N) -> N {
(a ^ b) % 16_777_216
}
let a = mix_prune(n * 64, n);
let b = mix_prune(a / 32, a);
mix_prune(b * 2048, b)
}
fn nth_secret_number(n: N, n_th: usize) -> N {
if n_th == 0 {
n
} else {
nth_secret_number(next(n), n_th - 1)
}
}
fn create_bananas_and_diffs(n: N, how_many: usize) -> (Vec<N>, Vec<Diff>) {
let mut bananas = Vec::with_capacity(how_many + 1);
bananas.push(n);
for i in 0 .. how_many {
bananas.push(next(bananas[i]));
}
let mut diffs = Vec::with_capacity(how_many);
for i in 0 .. how_many {
diffs.push((bananas[i + 1] % 10) as Diff - (bananas[i] % 10) as Diff);
}
bananas.iter_mut().for_each(|b| *b %= 10);
(bananas, diffs)
}
fn create_banana_map(n: N) -> HashMap<Sequence, N> {
let (bananas, all_diffs) = create_bananas_and_diffs(n, 2000);
let mut results = HashMap::new();
for i in 0 .. (all_diffs.len() - SEQUENCE_LENGTH) {
let slice = &all_diffs[i .. (i + SEQUENCE_LENGTH)];
let diffs: Sequence = slice.try_into().expect("Slice with incorrect length");
let result = bananas[i + SEQUENCE_LENGTH];
results.entry(diffs).or_insert(result);
}
results
}
fn solve_1(data: &Input) -> N {
data.iter().map(|n| nth_secret_number(*n, 2000)).sum()
}
fn solve_2(data: &Input) -> N {
let mut results: HashMap<Sequence, N> = HashMap::new();
for n in data {
let banana_map = create_banana_map(*n);
for (k, v) in banana_map {
let new_value = results.entry(k).or_insert(0);
*new_value += v;
}
}
*results.values().max().expect("No max value")
}
fn main() -> Result<(), Error> {
let data = parse(DATA)?;
let result_1 = solve_1(&data);
println!("Part 1: {result_1}");
let result_2 = solve_2(&data);
println!("Part 2: {result_2}");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_DATA_0: &str = include_str!("../../resources/22-test-00.txt");
const TEST_DATA_1: &str = include_str!("../../resources/22-test-01.txt");
fn test_data_0() -> Input {
parse(TEST_DATA_0).unwrap()
}
fn test_data_1() -> Input {
parse(TEST_DATA_1).unwrap()
}
fn real_data() -> Input {
parse(DATA).unwrap()
}
#[test]
fn test_solve_1_test() {
assert_eq!(solve_1(&test_data_0()), 37_327_623);
}
#[test]
fn test_solve_1_real() {
assert_eq!(solve_1(&real_data()), 13_022_553_808);
}
#[test]
fn test_solve_2_test() {
assert_eq!(solve_2(&test_data_1()), 7 + 7 + 9);
}
#[test]
#[ignore]
fn test_solve_2_real() {
assert_eq!(solve_2(&real_data()), 1555);
}
}