Skip to content

Commit 01192a3

Browse files
committed
Add day11a
1 parent 105ab4b commit 01192a3

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

day11a/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

day11a/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "day11a"
3+
version = "0.1.0"
4+
authors = ["Tim Visee <3a4fb3964f@sinenomine.email>"]
5+
edition = "2021"
6+
7+
[lib]
8+
path = "src/main.rs"
9+
10+
[dependencies]
11+
itertools = "0.10"

day11a/input.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Monkey 0:
2+
Starting items: 66, 71, 94
3+
Operation: new = old * 5
4+
Test: divisible by 3
5+
If true: throw to monkey 7
6+
If false: throw to monkey 4
7+
8+
Monkey 1:
9+
Starting items: 70
10+
Operation: new = old + 6
11+
Test: divisible by 17
12+
If true: throw to monkey 3
13+
If false: throw to monkey 0
14+
15+
Monkey 2:
16+
Starting items: 62, 68, 56, 65, 94, 78
17+
Operation: new = old + 5
18+
Test: divisible by 2
19+
If true: throw to monkey 3
20+
If false: throw to monkey 1
21+
22+
Monkey 3:
23+
Starting items: 89, 94, 94, 67
24+
Operation: new = old + 2
25+
Test: divisible by 19
26+
If true: throw to monkey 7
27+
If false: throw to monkey 0
28+
29+
Monkey 4:
30+
Starting items: 71, 61, 73, 65, 98, 98, 63
31+
Operation: new = old * 7
32+
Test: divisible by 11
33+
If true: throw to monkey 5
34+
If false: throw to monkey 6
35+
36+
Monkey 5:
37+
Starting items: 55, 62, 68, 61, 60
38+
Operation: new = old + 7
39+
Test: divisible by 5
40+
If true: throw to monkey 2
41+
If false: throw to monkey 1
42+
43+
Monkey 6:
44+
Starting items: 93, 91, 69, 64, 72, 89, 50, 71
45+
Operation: new = old + 1
46+
Test: divisible by 13
47+
If true: throw to monkey 5
48+
If false: throw to monkey 2
49+
50+
Monkey 7:
51+
Starting items: 76, 50
52+
Operation: new = old * old
53+
Test: divisible by 7
54+
If true: throw to monkey 4
55+
If false: throw to monkey 6

day11a/src/main.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#![feature(box_syntax)]
2+
3+
use itertools::Itertools;
4+
5+
struct Monkey {
6+
bag: Vec<usize>,
7+
op: Box<dyn Fn(usize) -> usize>,
8+
div: usize,
9+
yay: usize,
10+
nay: usize,
11+
ins: usize,
12+
}
13+
14+
pub fn main() {
15+
let mut m: Vec<_> = include_str!("../input.txt")
16+
.split("\n\n")
17+
.map(|m| {
18+
let l: Vec<_> = m.lines().map(|l| l.split(": ").last().unwrap()).collect();
19+
Monkey {
20+
bag: l[1].split(", ").map(|n| n.parse().unwrap()).collect(),
21+
op: {
22+
let op: Vec<_> = l[2].rsplit_once("= ").unwrap().1.split(' ').collect();
23+
match op[2] {
24+
"old" => box |old| old * old,
25+
b => match (op[1], b.parse::<usize>().unwrap()) {
26+
("+", n) => box move |old| old + n,
27+
("*", n) => box move |old| old * n,
28+
_ => unreachable!(),
29+
},
30+
}
31+
},
32+
div: l[3].rsplit_once(' ').unwrap().1.parse().unwrap(),
33+
yay: l[4].rsplit_once(' ').unwrap().1.parse().unwrap(),
34+
nay: l[5].rsplit_once(' ').unwrap().1.parse().unwrap(),
35+
ins: 0,
36+
}
37+
})
38+
.collect();
39+
let (mo, mut bags): (usize, _) = (m.iter().map(|m| m.div).product(), vec![vec![]; m.len()]);
40+
41+
(0..20).for_each(|_| {
42+
m.iter_mut().enumerate().for_each(|(i, m)| {
43+
m.bag.append(&mut bags[i]);
44+
m.bag.drain(0..).for_each(|mut n| {
45+
n = (m.op)(n) / 3 % mo;
46+
bags[if n % m.div == 0 { m.yay } else { m.nay }].push(n);
47+
m.ins += 1;
48+
});
49+
});
50+
});
51+
52+
println!(
53+
"{}",
54+
m.iter()
55+
.map(|m| m.ins)
56+
.sorted_unstable_by(|a, b| b.cmp(a))
57+
.take(2)
58+
.product::<usize>()
59+
);
60+
}

0 commit comments

Comments
 (0)