Skip to content

Commit cb8c43f

Browse files
committed
Day 11 - Monkey in the Middle 🐒
1 parent d98256d commit cb8c43f

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

.github/workflows/Day-11.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Day-11
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
paths:
7+
- '**11*'
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Build
16+
run: cargo build --release --verbose
17+
- name: Run
18+
run: time target/release/aoc 11

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This is primarily a learning experience, and the code may not be following best
1818
| 8 | Treetop Tree House | [src/day_08.rs](src/day_08.rs) | [src/day_08.data](src/day_08.data) | `0m0.002s` | [![Day-08](https://github.com/leifgehrmann/advent-of-code-2022/actions/workflows/Day-08.yml/badge.svg?branch=main)](https://github.com/leifgehrmann/advent-of-code-2022/actions/workflows/Day-08.yml?query=branch%3Amain)
1919
| 9 | Rope Bridge | | | |
2020
| 10 | Cathode-Ray Tube | [src/day_10.rs](src/day_10.rs) | [src/day_10.data](src/day_10.data) | `0m0.003s` | [![Day-10](https://github.com/leifgehrmann/advent-of-code-2022/actions/workflows/Day-10.yml/badge.svg?branch=main)](https://github.com/leifgehrmann/advent-of-code-2022/actions/workflows/Day-10.yml?query=branch%3Amain)
21+
| 11 | Monkey in the Middle | [src/day_11.rs](src/day_11.rs) | [src/day_11.data](src/day_11.data) | | [![Day-11](https://github.com/leifgehrmann/advent-of-code-2022/actions/workflows/Day-11.yml/badge.svg?branch=main)](https://github.com/leifgehrmann/advent-of-code-2022/actions/workflows/Day-11.yml?query=branch%3Amain)
2122

2223
_The measured execution time in GitHub Actions_
2324

src/day_11.data

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

src/day_11.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use crate::input_reader;
2+
3+
#[derive(PartialEq, Eq, Debug)]
4+
enum Operator {
5+
Add,
6+
Mul
7+
}
8+
9+
struct Monkey {
10+
starting_items: Vec<i32>,
11+
operator: Operator,
12+
operand: Option<i32>, // If null, the operand is 'old'.
13+
test_divisible: i32,
14+
throw_to_monkey_if_true: usize,
15+
throw_to_monkey_if_false: usize
16+
}
17+
18+
fn part1(monkeys: &Vec<Monkey>) {
19+
let mut monkey_states: Vec<Vec<i32>> = vec![vec![]; monkeys.len()];
20+
let mut monkey_inspections: Vec<i32> = vec![0; monkeys.len()];
21+
// copy starting items into state
22+
for m in 0..monkeys.len() {
23+
let cloned_state = &mut monkeys[m].starting_items.clone();
24+
monkey_states[m].append(cloned_state)
25+
}
26+
27+
for _ in 0..20 {
28+
for m in 0..monkeys.len() {
29+
let monkey = &monkeys[m];
30+
let monkey_items = monkey_states[m].clone();
31+
for item in monkey_items {
32+
// Increase monkey inspection.
33+
monkey_inspections[m] += 1;
34+
35+
// Calculate new worry.
36+
let mut worry = item.clone();
37+
let operand = monkey.operand.unwrap_or(worry);
38+
match monkey.operator {
39+
Operator::Add => worry = worry + operand,
40+
Operator::Mul => worry = worry * operand
41+
}
42+
worry = worry / 3;
43+
44+
// Throw to monkey.
45+
if worry % monkey.test_divisible == 0 {
46+
monkey_states[monkey.throw_to_monkey_if_true].push(worry);
47+
} else {
48+
monkey_states[monkey.throw_to_monkey_if_false].push(worry);
49+
}
50+
}
51+
52+
monkey_states[m] = vec![]
53+
}
54+
}
55+
56+
monkey_inspections.sort();
57+
monkey_inspections.reverse();
58+
println!("Part 1: {}", monkey_inspections[0] * monkey_inspections[1]);
59+
}
60+
61+
pub fn run() {
62+
let input = input_reader::read_file_in_cwd("src/day_11.data");
63+
64+
let lines: Vec<&str> = input.split("\n\n").collect();
65+
let monkeys: Vec<Monkey> = lines.iter().map(|&val| {
66+
let lines: Vec<&str> = val.split("\n").collect();
67+
// Parse items
68+
let starting_items: Vec<i32> = lines[1]
69+
.replace(",", "")
70+
.split(" ").skip(4)
71+
.map(|i| { i.parse::<i32>().unwrap() }).collect();
72+
// Parse operation
73+
let operation_str: Vec<&str> = lines[2].split(" ").collect();
74+
let operator = match operation_str[6] {
75+
"+" => Operator::Add,
76+
_ => Operator::Mul
77+
};
78+
let operand = operation_str[7].parse::<i32>().ok();
79+
// Parse test
80+
let test_str: Vec<&str> = lines[3].split(" ").collect();
81+
let test_divisible = test_str[5].parse::<i32>().unwrap();
82+
// Parse throws
83+
let throw_true_str: Vec<&str> = lines[4].split(" ").collect();
84+
let throw_false_str: Vec<&str> = lines[5].split(" ").collect();
85+
let throw_true = throw_true_str[9].parse::<usize>().unwrap();
86+
let throw_false = throw_false_str[9].parse::<usize>().unwrap();
87+
88+
return Monkey {
89+
starting_items,
90+
operator,
91+
operand,
92+
test_divisible,
93+
throw_to_monkey_if_true: throw_true,
94+
throw_to_monkey_if_false: throw_false
95+
}
96+
}).collect();
97+
98+
part1(&monkeys);
99+
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod day_06;
99
mod day_08;
1010
// mod day_09;
1111
mod day_10;
12+
mod day_11;
1213

1314
fn main() {
1415
let day: String = std::env::args().nth(1).expect(
@@ -27,6 +28,7 @@ fn main() {
2728
"08" => day_08::run(),
2829
// "09" => day_09::run(),
2930
"10" => day_10::run(),
31+
"11" => day_11::run(),
3032
_ => println!("No valid day given. Possible options are: 01-25."),
3133
};
3234
}

0 commit comments

Comments
 (0)