-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
Monkey 0: | ||
Starting items: 98, 70, 75, 80, 84, 89, 55, 98 | ||
Operation: new = old * 2 | ||
Test: divisible by 11 | ||
If true: throw to monkey 1 | ||
If false: throw to monkey 4 | ||
|
||
Monkey 1: | ||
Starting items: 59 | ||
Operation: new = old * old | ||
Test: divisible by 19 | ||
If true: throw to monkey 7 | ||
If false: throw to monkey 3 | ||
|
||
Monkey 2: | ||
Starting items: 77, 95, 54, 65, 89 | ||
Operation: new = old + 6 | ||
Test: divisible by 7 | ||
If true: throw to monkey 0 | ||
If false: throw to monkey 5 | ||
|
||
Monkey 3: | ||
Starting items: 71, 64, 75 | ||
Operation: new = old + 2 | ||
Test: divisible by 17 | ||
If true: throw to monkey 6 | ||
If false: throw to monkey 2 | ||
|
||
Monkey 4: | ||
Starting items: 74, 55, 87, 98 | ||
Operation: new = old * 11 | ||
Test: divisible by 3 | ||
If true: throw to monkey 1 | ||
If false: throw to monkey 7 | ||
|
||
Monkey 5: | ||
Starting items: 90, 98, 85, 52, 91, 60 | ||
Operation: new = old + 7 | ||
Test: divisible by 5 | ||
If true: throw to monkey 0 | ||
If false: throw to monkey 4 | ||
|
||
Monkey 6: | ||
Starting items: 99, 51 | ||
Operation: new = old + 1 | ||
Test: divisible by 13 | ||
If true: throw to monkey 5 | ||
If false: throw to monkey 2 | ||
|
||
Monkey 7: | ||
Starting items: 98, 94, 59, 76, 51, 65, 75 | ||
Operation: new = old + 5 | ||
Test: divisible by 2 | ||
If true: throw to monkey 3 | ||
If false: throw to monkey 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Monkey 0: | ||
Starting items: 79, 98 | ||
Operation: new = old * 19 | ||
Test: divisible by 23 | ||
If true: throw to monkey 2 | ||
If false: throw to monkey 3 | ||
|
||
Monkey 1: | ||
Starting items: 54, 65, 75, 74 | ||
Operation: new = old + 6 | ||
Test: divisible by 19 | ||
If true: throw to monkey 2 | ||
If false: throw to monkey 0 | ||
|
||
Monkey 2: | ||
Starting items: 79, 60, 97 | ||
Operation: new = old * old | ||
Test: divisible by 13 | ||
If true: throw to monkey 1 | ||
If false: throw to monkey 3 | ||
|
||
Monkey 3: | ||
Starting items: 74 | ||
Operation: new = old + 3 | ||
Test: divisible by 17 | ||
If true: throw to monkey 0 | ||
If false: throw to monkey 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
#[aoc_generator(day11)] | ||
pub fn input_generator(input: &str) -> (Vec<Vec<usize>>, Vec<String>, Vec<usize>, Vec<usize>, Vec<usize>, Vec<usize>) { | ||
let items = input | ||
.split("\n\n") | ||
.map(|monkey| { | ||
let lines: Vec<&str> = monkey.lines().collect(); | ||
|
||
lines[1] | ||
.trim() | ||
.strip_prefix("Starting items: ") | ||
.unwrap() | ||
.split(", ") | ||
.map(|item| item.parse().unwrap()) | ||
.collect() | ||
}) | ||
.collect(); | ||
|
||
let operation_types = input | ||
.split("\n\n") | ||
.map(|monkey| { | ||
let lines: Vec<&str> = monkey.lines().collect(); | ||
|
||
let mut operation_info = lines[2] | ||
.trim() | ||
.strip_prefix("Operation: new = old ") | ||
.unwrap() | ||
.split_whitespace(); | ||
|
||
let operation_info = ( | ||
operation_info.next().unwrap(), | ||
operation_info.next().unwrap(), | ||
); | ||
|
||
let operation_info = match operation_info { | ||
("*", "old") => ("^", 2), | ||
(t, v) => (t, v.parse().unwrap()), | ||
}; | ||
|
||
operation_info.0.to_string() | ||
}) | ||
.collect(); | ||
|
||
let operation_values = input | ||
.split("\n\n") | ||
.map(|monkey| { | ||
let lines: Vec<&str> = monkey.lines().collect(); | ||
|
||
let mut operation_info = lines[2] | ||
.trim() | ||
.strip_prefix("Operation: new = old ") | ||
.unwrap() | ||
.split_whitespace(); | ||
|
||
let operation_info = ( | ||
operation_info.next().unwrap(), | ||
operation_info.next().unwrap(), | ||
); | ||
|
||
let operation_info = match operation_info { | ||
("*", "old") => ("^", 2), | ||
(t, v) => (t, v.parse().unwrap()), | ||
}; | ||
|
||
operation_info.1 | ||
}) | ||
.collect(); | ||
|
||
let tests = input | ||
.split("\n\n") | ||
.map(|monkey| { | ||
let lines: Vec<&str> = monkey.lines().collect(); | ||
|
||
lines[3] | ||
.trim() | ||
.strip_prefix("Test: divisible by ") | ||
.unwrap() | ||
.parse() | ||
.unwrap() | ||
}) | ||
.collect(); | ||
|
||
let true_monkeys = input | ||
.split("\n\n") | ||
.map(|monkey| { | ||
let lines: Vec<&str> = monkey.lines().collect(); | ||
|
||
lines[4] | ||
.trim() | ||
.strip_prefix("If true: throw to monkey ") | ||
.unwrap() | ||
.parse() | ||
.unwrap() | ||
}) | ||
.collect(); | ||
|
||
let false_monkeys = input | ||
.split("\n\n") | ||
.map(|monkey| { | ||
let lines: Vec<&str> = monkey.lines().collect(); | ||
|
||
lines[5] | ||
.trim() | ||
.strip_prefix("If false: throw to monkey ") | ||
.unwrap() | ||
.parse() | ||
.unwrap() | ||
}) | ||
.collect(); | ||
|
||
( | ||
items, | ||
operation_types, | ||
operation_values, | ||
tests, | ||
true_monkeys, | ||
false_monkeys, | ||
) | ||
} | ||
|
||
#[aoc(day11, part1)] | ||
pub fn solve_part1(input: &(Vec<Vec<usize>>, Vec<String>, Vec<usize>, Vec<usize>, Vec<usize>, Vec<usize>)) -> usize { | ||
let mut items: Vec<Vec<usize>> = vec![Vec::new(); input.0.len()]; | ||
|
||
for (i, items_vec) in input.0.iter().enumerate() { | ||
for item in items_vec { | ||
items[i].push(*item); | ||
} | ||
} | ||
|
||
let operation_types = &input.1; | ||
let operation_values = &input.2; | ||
let tests = &input.3; | ||
let true_monkeys = &input.4; | ||
let false_monkeys = &input.5; | ||
|
||
let mut inspections = vec![0; items.len()]; | ||
|
||
for _ in 0..20 { | ||
for i in 0..items.len() { | ||
inspections[i] += items[i].len(); | ||
|
||
for j in 0..items[i].len() { | ||
let item = items[i][j]; | ||
|
||
let new_item = match (operation_types[i].as_str(), operation_values[i]) { | ||
("+", v) => item + v, | ||
("*", v) => item * v, | ||
("^", _) => item * item, | ||
_ => item, | ||
}; | ||
|
||
let new_item = new_item / 3; | ||
|
||
if new_item % tests[i] == 0 { | ||
items[true_monkeys[i]].push(new_item); | ||
} else { | ||
items[false_monkeys[i]].push(new_item); | ||
} | ||
} | ||
|
||
items[i].clear(); | ||
} | ||
} | ||
|
||
inspections.sort(); | ||
inspections.reverse(); | ||
|
||
inspections[0] * inspections[1] | ||
} | ||
|
||
#[aoc(day11, part2)] | ||
pub fn solve_part2(input: &(Vec<Vec<usize>>, Vec<String>, Vec<usize>, Vec<usize>, Vec<usize>, Vec<usize>)) -> usize { | ||
let mut items: Vec<Vec<usize>> = vec![Vec::new(); input.0.len()]; | ||
|
||
for (i, items_vec) in input.0.iter().enumerate() { | ||
for item in items_vec { | ||
items[i].push(*item); | ||
} | ||
} | ||
|
||
let operation_types = &input.1; | ||
let operation_values = &input.2; | ||
let tests = &input.3; | ||
let true_monkeys = &input.4; | ||
let false_monkeys = &input.5; | ||
|
||
let divisor: usize = tests | ||
.iter() | ||
.product(); | ||
|
||
let mut inspections = vec![0; items.len()]; | ||
|
||
for _ in 0..10_000 { | ||
for i in 0..items.len() { | ||
inspections[i] += items[i].len(); | ||
|
||
for j in 0..items[i].len() { | ||
let item = items[i][j]; | ||
|
||
let new_item = match (operation_types[i].as_str(), operation_values[i]) { | ||
("+", v) => item + v, | ||
("*", v) => item * v, | ||
("^", _) => item * item, | ||
_ => item, | ||
}; | ||
|
||
let new_item = new_item % divisor; | ||
|
||
if new_item % tests[i] == 0 { | ||
items[true_monkeys[i]].push(new_item); | ||
} else { | ||
items[false_monkeys[i]].push(new_item); | ||
} | ||
} | ||
|
||
items[i].clear(); | ||
} | ||
} | ||
|
||
inspections.sort(); | ||
inspections.reverse(); | ||
|
||
inspections[0] * inspections[1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,6 @@ pub mod day7; | |
pub mod day8; | ||
pub mod day9; | ||
pub mod day10; | ||
pub mod day11; | ||
|
||
aoc_lib!{ year = 2022 } |