Skip to content

Commit fdd8a3d

Browse files
committed
Add day 11
1 parent 3e15ddd commit fdd8a3d

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

day11_input.txt

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Monkey 0:
2+
Starting items: 50, 70, 89, 75, 66, 66
3+
Operation: new = old * 5
4+
Test: divisible by 2
5+
If true: throw to monkey 2
6+
If false: throw to monkey 1
7+
8+
Monkey 1:
9+
Starting items: 85
10+
Operation: new = old * old
11+
Test: divisible by 7
12+
If true: throw to monkey 3
13+
If false: throw to monkey 6
14+
15+
Monkey 2:
16+
Starting items: 66, 51, 71, 76, 58, 55, 58, 60
17+
Operation: new = old + 1
18+
Test: divisible by 13
19+
If true: throw to monkey 1
20+
If false: throw to monkey 3
21+
22+
Monkey 3:
23+
Starting items: 79, 52, 55, 51
24+
Operation: new = old + 6
25+
Test: divisible by 3
26+
If true: throw to monkey 6
27+
If false: throw to monkey 4
28+
29+
Monkey 4:
30+
Starting items: 69, 92
31+
Operation: new = old * 17
32+
Test: divisible by 19
33+
If true: throw to monkey 7
34+
If false: throw to monkey 5
35+
36+
Monkey 5:
37+
Starting items: 71, 76, 73, 98, 67, 79, 99
38+
Operation: new = old + 8
39+
Test: divisible by 5
40+
If true: throw to monkey 0
41+
If false: throw to monkey 2
42+
43+
Monkey 6:
44+
Starting items: 82, 76, 69, 69, 57
45+
Operation: new = old + 7
46+
Test: divisible by 11
47+
If true: throw to monkey 7
48+
If false: throw to monkey 4
49+
50+
Monkey 7:
51+
Starting items: 65, 79, 86
52+
Operation: new = old + 5
53+
Test: divisible by 17
54+
If true: throw to monkey 5
55+
If false: throw to monkey 0

day11a/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "day11a"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
regex = "1"

day11a/src/main.rs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use regex::Regex;
2+
use std::collections::VecDeque;
3+
use std::io::Read;
4+
5+
enum Operation {
6+
Add(u32),
7+
Mult(u32),
8+
Square,
9+
}
10+
11+
struct Monkey {
12+
items: VecDeque<u32>,
13+
op: Operation,
14+
divisor: u32,
15+
t: usize,
16+
f: usize,
17+
inspections: u32,
18+
}
19+
20+
fn main() {
21+
let mut input = String::new();
22+
std::io::stdin().read_to_string(&mut input).unwrap();
23+
24+
let re = Regex::new(concat!(
25+
r"(?m)^Monkey (\d+):\n Starting items: (\d+(?:, \d+)*)\n Operation: new = old (?:\* (?:(\d+)|(old))|\+ ",
26+
r"(\d+))\n Test: divisible by (\d+)\n If true: throw to monkey (\d+)\n If false: throw to monkey (\d+)$",
27+
)).unwrap();
28+
29+
let mut monkeys: Vec<_> = re.captures_iter(&input).enumerate().map(|(n, m)| {
30+
assert_eq!(m[1].parse::<usize>().unwrap(), n);
31+
32+
Monkey {
33+
items: m[2].split(", ").map(|num| num.parse().unwrap()).collect(),
34+
op: if let Some(factor) = m.get(3) {
35+
Operation::Mult(factor.as_str().parse().unwrap())
36+
} else if let Some(_) = m.get(4) {
37+
Operation::Square
38+
} else if let Some(term) = m.get(5) {
39+
Operation::Add(term.as_str().parse().unwrap())
40+
} else {
41+
panic!()
42+
},
43+
divisor: m[6].parse().unwrap(),
44+
t: m[7].parse().unwrap(),
45+
f: m[8].parse().unwrap(),
46+
inspections: 0,
47+
}
48+
}).collect();
49+
50+
for _ in 0..20 {
51+
for i in 0..monkeys.len() {
52+
while let Some(item) = monkeys[i].items.pop_front() {
53+
let monkey = &monkeys[i];
54+
let item = match monkey.op {
55+
Operation::Mult(factor) => item * factor,
56+
Operation::Square => item * item,
57+
Operation::Add(term) => item + term,
58+
} / 3;
59+
let target = if item % monkey.divisor == 0 { monkey.t } else { monkey.f };
60+
monkeys[i].inspections += 1;
61+
monkeys[target].items.push_back(item);
62+
}
63+
}
64+
}
65+
66+
let mut top = [0; 2];
67+
for monkey in &monkeys {
68+
let i = top.iter_mut().min().unwrap();
69+
if monkey.inspections > *i {
70+
*i = monkey.inspections;
71+
}
72+
}
73+
println!("{}", top.iter().product::<u32>());
74+
}

day11b/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "day11b"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
regex = "1"

day11b/src/main.rs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use regex::Regex;
2+
use std::collections::VecDeque;
3+
use std::io::Read;
4+
5+
enum Operation {
6+
Add(u64),
7+
Mult(u64),
8+
Square,
9+
}
10+
11+
struct Monkey {
12+
items: VecDeque<u64>,
13+
op: Operation,
14+
divisor: u64,
15+
t: usize,
16+
f: usize,
17+
inspections: u64,
18+
}
19+
20+
fn main() {
21+
let mut input = String::new();
22+
std::io::stdin().read_to_string(&mut input).unwrap();
23+
24+
let re = Regex::new(concat!(
25+
r"(?m)^Monkey (\d+):\n Starting items: (\d+(?:, \d+)*)\n Operation: new = old (?:\* (?:(\d+)|(old))|\+ ",
26+
r"(\d+))\n Test: divisible by (\d+)\n If true: throw to monkey (\d+)\n If false: throw to monkey (\d+)$",
27+
)).unwrap();
28+
29+
let mut monkeys: Vec<_> = re.captures_iter(&input).enumerate().map(|(n, m)| {
30+
assert_eq!(m[1].parse::<usize>().unwrap(), n);
31+
32+
Monkey {
33+
items: m[2].split(", ").map(|num| num.parse().unwrap()).collect(),
34+
op: if let Some(factor) = m.get(3) {
35+
Operation::Mult(factor.as_str().parse().unwrap())
36+
} else if let Some(_) = m.get(4) {
37+
Operation::Square
38+
} else if let Some(term) = m.get(5) {
39+
Operation::Add(term.as_str().parse().unwrap())
40+
} else {
41+
panic!()
42+
},
43+
divisor: m[6].parse().unwrap(),
44+
t: m[7].parse().unwrap(),
45+
f: m[8].parse().unwrap(),
46+
inspections: 0,
47+
}
48+
}).collect();
49+
let modulo = monkeys.iter().fold(1, |acc, monkey|
50+
if acc % monkey.divisor == 0 { acc } else { acc * monkey.divisor }
51+
);
52+
53+
for _ in 0..10000 {
54+
for i in 0..monkeys.len() {
55+
while let Some(item) = monkeys[i].items.pop_front() {
56+
let monkey = &monkeys[i];
57+
let item = match monkey.op {
58+
Operation::Mult(factor) => item * factor,
59+
Operation::Square => item * item,
60+
Operation::Add(term) => item + term,
61+
} % modulo;
62+
let target = if item % monkey.divisor == 0 { monkey.t } else { monkey.f };
63+
monkeys[i].inspections += 1;
64+
monkeys[target].items.push_back(item);
65+
}
66+
}
67+
}
68+
69+
let mut top = [0; 2];
70+
for monkey in &monkeys {
71+
let i = top.iter_mut().min().unwrap();
72+
if monkey.inspections > *i {
73+
*i = monkey.inspections;
74+
}
75+
}
76+
println!("{}", top.iter().product::<u64>());
77+
}

0 commit comments

Comments
 (0)