-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_11.rs
184 lines (170 loc) · 5.76 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
use std::collections::HashMap;
use crate::read_input;
use regex::Regex;
struct Monkey {
id: i64,
items: Vec<i64>,
operation: Box<dyn Fn (i64) -> i64>,
div_test: i64,
true_target: i64,
false_target: i64,
should_divide_by_3: bool,
master_modulo: Option<i64>,
interact_count: i64
}
impl Monkey {
fn take_turn(&mut self) -> HashMap<i64, Vec<i64>>{
// return map from monkey id to list of items to be passed to that monkey
let mut target_monkeys = HashMap::<i64, Vec<i64>>::new();
for item in &self.items {
self.interact_count += 1;
let mut new_val: i64 = (self.operation)(*item);
if self.should_divide_by_3 {
new_val = new_val / 3;
}
new_val = match self.master_modulo {
Some(modulo) => new_val % modulo,
None => new_val,
};
if new_val % self.div_test == 0 {
target_monkeys
.entry(self.true_target)
.or_insert(vec![])
.push(new_val);
} else {
target_monkeys
.entry(self.false_target)
.or_insert(vec![])
.push(new_val);
}
}
self.items = vec![];
target_monkeys
}
}
fn get_first_int(string: &str) -> i64 {
let re = Regex::new(r"\d+").unwrap();
for capture in re.captures_iter(string) {
return capture[0].parse().unwrap();
}
panic!("No number found")
}
fn get_all_ints(string: &str) -> Vec<i64>{
let re = Regex::new(r"\d+").unwrap();
let mut numbers = Vec::new();
for capture in re.captures_iter(string) {
let number: i64 = capture[0].parse().unwrap();
numbers.push(number);
}
numbers
}
fn get_operation(operation_str: &str) -> Box<dyn Fn (i64) -> i64> {
let operation_segment = operation_str.trim().split(" = ").collect::<Vec<&str>>()[1];
match operation_segment.split(' ').collect::<Vec<&str>>()[..] {
["old", op, "old"] => {
match op {
"*" => Box::new(move |old: i64| old * old),
"+" => Box::new(move |old: i64| old + old),
_ => panic!(),
}
},
["old", op, num] => {
let val = num.parse::<i64>().expect("Number");
match op {
"*" => Box::new(move |old: i64| old * val),
"+" => Box::new(move |old: i64| old + val),
_ => panic!(),
}
},
_ => panic!(),
}
}
fn do_rounds(round_count: i32, should_divide_by_3: bool) -> Vec<Monkey> {
let input = read_input::read("inputs/11.txt");
let mut monkeys: Vec<Monkey> = vec![];
let mut has_next: bool = true;
let mut lines = input.split("\n");
while has_next {
match lines.next() {
Some(line) => {
let id = get_first_int(line);
let items = get_all_ints(lines.next().expect("Items line"));
let operation = get_operation(lines.next().expect("Operation line"));
let div_test = get_first_int(lines.next().expect("Test line"));
let true_target = get_first_int(lines.next().expect("True target line"));
let false_target = get_first_int(lines.next().expect("False target line"));
lines.next().expect("Blank line");
let m = Monkey {
id, items, operation, div_test, true_target, false_target, should_divide_by_3, master_modulo: None, interact_count: 0
};
monkeys.push(m);
},
None => has_next = false,
}
}
if !should_divide_by_3 {
let master_modulo = monkeys
.iter()
.map(|m| m.div_test)
.product();
for monkey in &mut monkeys {
monkey.master_modulo = Some(master_modulo)
}
}
monkeys.sort_by(|a, b| a.id.cmp(&b.id));
for _ in 0..round_count {
// map of target IDs to items thrown to that target
let mut targets_items: HashMap<i64, Vec<i64>> = HashMap::new();
for thrower in &mut monkeys {
// if any items have already been thrown to this monkey this round, extend the monkey's
// list of items with those items
thrower.items.extend(targets_items
.get(&thrower.id)
.unwrap_or(&vec![])
);
// clear list of items already thrown to this monkey this round
targets_items.insert(thrower.id, vec![]);
// populate lists in map
for (target, items) in thrower.take_turn() {
targets_items
.entry(target)
.or_insert(vec![])
.extend(items);
}
}
// after all monkeys have thrown, all monkeys catch items thrown to them by throwers which
// follow them in turn order
for (target, items) in targets_items {
monkeys
.iter_mut()
.filter(|m|m.id == target)
.next()
.expect("Target monkey")
.items
.extend(items)
}
}
monkeys
}
fn get_monkey_business(mut monkeys: Vec<Monkey>) -> i64 {
monkeys.sort_by(|a, b| b.interact_count.cmp(&a.interact_count));
monkeys
.into_iter()
.take(2)
.map(|m| m.interact_count)
.product::<i64>()
}
fn part_1() {
let monkeys = do_rounds(20, true);
println!("Part 1: {}", get_monkey_business(monkeys));
}
fn part_2() {
let monkeys = do_rounds(10000, false);
println!("Part 2: {}", get_monkey_business(monkeys));
}
pub fn solve() {
println!("Day 11");
part_1();
part_2();
println!();
}