-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-11.rs
223 lines (198 loc) · 5.32 KB
/
day-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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::{collections::VecDeque, error::Error, str::FromStr};
const INPUT: &str = include_str!("day-11.txt");
#[cfg(test)]
const INPUT_EX: &str = "Monkey 0:
Starting items: 79, 98
Operation: new = old * 19
Test: divisible by 23
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1";
aoc_2022::aoc! {
#[derive(Clone)]
struct Day11 {
monkeys: Vec<Monkey>,
modulo: usize,
}
self(input) {
let monkeys: Vec<Monkey> = input.split("\n\n").map(|s| s.parse()).collect::<Result<_,_>>()?;
let modulo = monkeys.iter().map(|m| m.test.test).product();
Ok(Self { monkeys, modulo })
}
part1 usize {
let mut day = self.clone();
for _ in 0..20 {
day.round(3);
}
Ok(day.monkey_biz())
}
part2 usize {
for _ in 0..10_000 {
self.round(1);
}
Ok(self.monkey_biz())
}
input = INPUT;
test day11_ex(INPUT_EX, 10605, 2713310158);
test day11(INPUT, 58056, 15048718170);
}
impl Day11 {
fn round(&mut self, worry_div: usize) {
let n = self.monkeys.len();
for m in 0..n {
while let Some((target, item)) = self.monkeys[m].inspect(worry_div, self.modulo) {
self.monkeys[target].receive(item);
}
}
}
fn monkey_biz(&self) -> usize {
let mut i: Vec<_> = self.monkeys.iter().map(|m| m.inspections).collect();
i.sort_unstable_by(|a, b| b.cmp(a));
i[0] * i[1]
}
}
#[derive(Clone)]
struct Monkey {
items: VecDeque<usize>,
op: Op,
test: Test,
inspections: usize,
}
impl Monkey {
fn inspect(&mut self, worry_div: usize, m: usize) -> Option<(usize, usize)> {
if let Some(x) = self.items.pop_front() {
let x = self.op.do_op(x) / worry_div % m;
self.inspections += 1;
let target = self.test.get_target_monkey(x);
Some((target, x))
} else {
None
}
}
fn receive(&mut self, item: usize) {
self.items.push_back(item);
}
}
impl FromStr for Monkey {
type Err = Box<dyn Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (m, s) = s.split_once('\n').ok_or("missing line")?;
let _ = m.strip_prefix("Monkey ").ok_or("expected monkey")?;
let (items, s) = s.split_once('\n').ok_or("missing line")?;
let items = items
.trim()
.strip_prefix("Starting items: ")
.ok_or("expected items")?
.split(", ")
.map(|i| i.parse())
.collect::<Result<_, _>>()?;
let (op, s) = s.split_once('\n').ok_or("missing line")?;
let op = op.parse()?;
let test = s.parse()?;
Ok(Monkey {
items,
op,
test,
inspections: 0,
})
}
}
#[derive(Clone, Copy)]
enum Op {
OldPlusX(usize),
OldTimesX(usize),
OldTimesOld,
}
impl Op {
fn do_op(&self, old: usize) -> usize {
match self {
Op::OldPlusX(x) => old + x,
Op::OldTimesX(x) => old * x,
Op::OldTimesOld => old * old,
}
}
}
impl FromStr for Op {
type Err = Box<dyn Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s
.strip_prefix(" Operation: new = old ")
.ok_or("invalid operation")?;
match &s[0..1] {
"+" => {
let s = s[1..].trim();
Ok(Op::OldPlusX(s.parse()?))
}
"*" => {
let s = s[1..].trim();
if s == "old" {
Ok(Op::OldTimesOld)
} else {
Ok(Op::OldTimesX(s.parse()?))
}
}
_ => Err("unknown op".into()),
}
}
}
#[derive(Clone, Copy)]
struct Test {
test: usize,
if_true: usize,
if_false: usize,
}
impl Test {
fn get_target_monkey(&self, n: usize) -> usize {
if n % self.test == 0 {
self.if_true
} else {
self.if_false
}
}
}
impl FromStr for Test {
type Err = Box<dyn Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut s = s.lines();
let test = s
.next()
.ok_or("expected line")?
.strip_prefix(" Test: divisible by ")
.ok_or("expected divisible by")?
.parse()?;
let if_true = s
.next()
.ok_or("expected line")?
.strip_prefix(" If true: throw to monkey ")
.ok_or("expected if true")?
.parse()?;
let if_false = s
.next()
.ok_or("expected line")?
.strip_prefix(" If false: throw to monkey ")
.ok_or("expected if false")?
.parse()?;
Ok(Self {
test,
if_true,
if_false,
})
}
}