-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.rs
87 lines (70 loc) · 2.08 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
79
80
81
82
83
84
85
86
87
//! [Day 22: Monkey Market](https://adventofcode.com/2024/day/22)
use rustc_hash::{FxHashMap, FxHashSet};
const fn next_secret(secret: i64) -> i64 {
let secret = (secret ^ (secret * 64)) % 16_777_216;
let secret = (secret ^ (secret / 32)) % 16_777_216;
(secret ^ (secret * 2048)) % 16_777_216
}
struct Puzzle {
initial_secrets: Vec<i64>,
}
impl Puzzle {
fn new(data: &str) -> Self {
Self {
initial_secrets: data.lines().map_while(|s| s.parse::<i64>().ok()).collect(),
}
}
/// Solve part one.
fn part1(&self) -> i64 {
self.initial_secrets
.iter()
.map(|&initial_secret| (0..2000).fold(initial_secret, |secret, _| next_secret(secret)))
.sum()
}
/// Solve part two.
fn part2(&self) -> i64 {
let mut bananas = FxHashMap::default();
for &initial_secret in &self.initial_secrets {
let mut prices = Vec::new();
let mut secret = initial_secret;
prices.push(secret % 10);
for _ in 0..2000 {
secret = next_secret(secret);
prices.push(secret % 10);
}
let mut seen = FxHashSet::default();
for p in prices.windows(5) {
let sequence = [p[1] - p[0], p[2] - p[1], p[3] - p[2], p[4] - p[3]];
if seen.insert(sequence) {
*bananas.entry(sequence).or_default() += p[4];
}
}
}
*bananas.values().max().unwrap()
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (i64, i64) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn test_part1() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part1(), 37327623);
}
#[test]
fn test_part2() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part2(), 23 + 1);
}
}