-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_11.rs
131 lines (108 loc) Β· 3.18 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
use std::collections::VecDeque;
use common::{solution, Answer};
solution!("Monkey in the Middle", 11);
fn part_a(input: &str) -> Answer {
let monkeys = parse_monkeys(input);
process(monkeys, 20, |x| x / 3).into()
}
fn part_b(input: &str) -> Answer {
let monkeys = parse_monkeys(input);
let magic = monkeys.iter().map(|x| x.test.divisor).product::<u64>();
process(monkeys, 10000, |x| x % magic).into()
}
struct Monkey {
items: VecDeque<u64>,
inspected: usize,
operation: Operation,
test: Test,
}
enum Operation {
Add(u64),
Multiply(u64),
Square,
}
struct Test {
divisor: u64,
// [true, false]
monkey: [usize; 2],
}
fn process(mut monkeys: Vec<Monkey>, iter: usize, proc: impl Fn(u64) -> u64) -> usize {
for _ in 0..iter {
for m in 0..monkeys.len() {
while let Some(item) = monkeys[m].items.pop_front() {
monkeys[m].inspected += 1;
let item = proc(monkeys[m].operation.process(item));
let goto = monkeys[m].test.process(item);
monkeys[goto].items.push_back(item);
}
}
}
monkeys.sort_unstable_by_key(|x| x.inspected);
monkeys.pop().unwrap().inspected * monkeys.pop().unwrap().inspected
}
fn parse_monkeys(raw: &str) -> Vec<Monkey> {
let mut out = Vec::new();
for i in raw.lines().collect::<Vec<_>>().chunks(7) {
let items = i[1]
.split_once(": ")
.unwrap()
.1
.split(", ")
.map(|x| x.parse::<u64>().unwrap())
.collect::<VecDeque<_>>();
let operation = Operation::parse(i[2].split_once(" = ").unwrap().1);
let test = Test::parse(&i[3..6]);
out.push(Monkey {
items,
inspected: 0,
operation,
test,
});
}
out
}
impl Operation {
fn parse(inp: &str) -> Self {
let mut parts = inp.split_whitespace();
assert_eq!(parts.next().unwrap(), "old");
let op = parts.next().unwrap();
let value = parts.next().unwrap();
match op {
"*" => match value {
"old" => Self::Square,
_ => Self::Multiply(value.parse::<u64>().unwrap()),
},
"+" => Self::Add(value.parse::<u64>().unwrap()),
_ => panic!("Unsuppored operation"),
}
}
fn process(&self, old: u64) -> u64 {
match self {
Self::Add(x) => old + x,
Self::Multiply(x) => old * x,
Self::Square => old * old,
}
}
}
impl Test {
fn parse(inp: &[&str]) -> Self {
let divisor = inp[0].split_once("by ").unwrap().1.parse::<u64>().unwrap();
let mut monkey = [0; 2];
for (i, line) in inp[1..].iter().enumerate() {
let monkey_id = line
.split_once("monkey ")
.unwrap()
.1
.parse::<usize>()
.unwrap();
monkey[i] = monkey_id;
}
Self { divisor, monkey }
}
fn process(&self, item: u64) -> usize {
if item % self.divisor == 0 {
return self.monkey[0];
}
self.monkey[1]
}
}