-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.rs
187 lines (168 loc) · 4.78 KB
/
11.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#![feature(test)]
use std::collections::hash_map::Entry;
use itertools::Itertools;
use num::Integer;
use rustc_hash::FxHashMap;
type Input = Vec<Monkey>;
#[derive(Debug)]
struct Monkey {
starting: Vec<u64>,
operation: Operation,
test: u64,
true_idx: usize,
false_idx: usize,
}
#[derive(Debug)]
enum Operation {
Add(Arg),
Mul(Arg),
}
impl Operation {
fn apply(&self, old: u64) -> u64 {
match self {
Operation::Add(x) => old + x.get(old),
Operation::Mul(x) => old * x.get(old),
}
}
}
#[derive(Debug)]
enum Arg {
Old,
Lit(u64),
}
impl Arg {
fn get(&self, old: u64) -> u64 {
match self {
Arg::Old => old,
&Arg::Lit(x) => x,
}
}
}
fn setup(input: &str) -> Input {
input
.trim()
.split("\n\n")
.map(|monkey| -> Option<Monkey> {
let mut lines = monkey.lines().skip(1);
let starting = lines
.next()?
.split(':')
.nth(1)?
.trim()
.split(',')
.map(|i| i.trim().parse().ok())
.collect::<Option<_>>()?;
let (arg, op) = lines.next()?.rsplit(' ').take(2).collect_tuple()?;
let arg = match arg.parse() {
Ok(n) => Arg::Lit(n),
Err(_) => Arg::Old,
};
let operation = match op {
"+" => Operation::Add(arg),
"*" => Operation::Mul(arg),
_ => panic!(),
};
let test = lines.next()?.rsplit(' ').next()?.parse().ok()?;
let true_idx = lines.next()?.rsplit(' ').next()?.parse().ok()?;
let false_idx = lines.next()?.rsplit(' ').next()?.parse().ok()?;
Some(Monkey {
starting,
operation,
test,
true_idx,
false_idx,
})
})
.collect::<Option<_>>()
.unwrap()
}
struct Solver<'a> {
monkeys: &'a Input,
rounds: usize,
div3: bool,
modulus: u64,
}
impl<'a> Solver<'a> {
fn new(monkeys: &'a Input, rounds: usize, div3: bool) -> Self {
Self {
monkeys,
rounds,
div3,
modulus: monkeys.iter().fold(1, |acc, monkey| acc.lcm(&monkey.test)),
}
}
fn simulate_round(&self, monkey: &mut usize, item: &mut u64, mut cnt: impl FnMut(usize)) {
let mut last = *monkey;
while last <= *monkey {
last = *monkey;
cnt(*monkey);
*item = self.monkeys[*monkey].operation.apply(*item);
if self.div3 {
*item /= 3;
}
*item %= self.modulus;
*monkey = if *item % self.monkeys[*monkey].test == 0 {
self.monkeys[*monkey].true_idx
} else {
self.monkeys[*monkey].false_idx
};
}
}
fn simulate_item(&mut self, mut monkey: usize, mut item: u64, cnt: &mut [u64]) {
// find cycle start and length
let mut _cnt = vec![0; self.monkeys.len()];
let mut m = monkey;
let mut i = item;
let mut seen = FxHashMap::default();
let mut iteration = 0;
while let Entry::Vacant(e) = seen.entry((m, i)) {
e.insert(iteration);
self.simulate_round(&mut m, &mut i, |i| _cnt[i] += 1);
iteration += 1;
if iteration == self.rounds {
(0..cnt.len()).for_each(|i| cnt[i] += _cnt[i]);
return;
}
}
let start = seen[&(m, i)];
let length = iteration - start;
// run optimized simulation
for _ in 0..start {
self.simulate_round(&mut monkey, &mut item, |i| {
cnt[i] += 1;
_cnt[i] -= 1;
});
}
for i in 0..cnt.len() {
cnt[i] += _cnt[i] * ((self.rounds - start) / length) as u64;
}
for _ in 0..(self.rounds - start) % length {
self.simulate_round(&mut monkey, &mut item, |i| cnt[i] += 1);
}
}
fn solve(mut self) -> u64 {
let mut cnt = vec![0; self.monkeys.len()];
for (m, monkey) in self.monkeys.iter().enumerate() {
for &item in &monkey.starting {
self.simulate_item(m, item, &mut cnt);
}
}
let (a, b) = cnt.iter().fold((0, 0), |(a, b), &x| {
if x > a {
(x, a)
} else if x > b {
(a, x)
} else {
(a, b)
}
});
a * b
}
}
fn part1(input: &Input) -> u64 {
Solver::new(input, 20, true).solve()
}
fn part2(input: &Input) -> u64 {
Solver::new(input, 10000, false).solve()
}
aoc::main!(2022, 11, ex: 1);