-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.rs
60 lines (55 loc) · 1.88 KB
/
main.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
#![feature(box_syntax)]
use itertools::Itertools;
struct Monkey {
bag: Vec<usize>,
op: Box<dyn Fn(usize) -> usize>,
div: usize,
yay: usize,
nay: usize,
ins: usize,
}
pub fn main() {
let mut m: Vec<_> = include_str!("../input.txt")
.split("\n\n")
.map(|m| {
let l: Vec<_> = m.lines().map(|l| l.split(": ").last().unwrap()).collect();
Monkey {
bag: l[1].split(", ").map(|n| n.parse().unwrap()).collect(),
op: {
let op: Vec<_> = l[2].rsplit_once("= ").unwrap().1.split(' ').collect();
match op[2] {
"old" => box |old| old * old,
b => match (op[1], b.parse::<usize>().unwrap()) {
("+", n) => box move |old| old + n,
("*", n) => box move |old| old * n,
_ => unreachable!(),
},
}
},
div: l[3].rsplit_once(' ').unwrap().1.parse().unwrap(),
yay: l[4].rsplit_once(' ').unwrap().1.parse().unwrap(),
nay: l[5].rsplit_once(' ').unwrap().1.parse().unwrap(),
ins: 0,
}
})
.collect();
let (mo, mut bags): (usize, _) = (m.iter().map(|m| m.div).product(), vec![vec![]; m.len()]);
(0..10_000).for_each(|_| {
m.iter_mut().enumerate().for_each(|(i, m)| {
m.bag.append(&mut bags[i]);
m.bag.drain(0..).for_each(|mut n| {
n = (m.op)(n) % mo;
bags[if n % m.div == 0 { m.yay } else { m.nay }].push(n);
m.ins += 1;
});
});
});
println!(
"{}",
m.iter()
.map(|m| m.ins)
.sorted_unstable_by(|a, b| b.cmp(a))
.take(2)
.product::<usize>()
);
}