-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rob Grant <rob.grant@nanoporetech.com>
- Loading branch information
1 parent
66261b4
commit 3375dad
Showing
2 changed files
with
141 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,83 @@ | ||
TEST = """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 """ | ||
|
||
REAL = """Monkey 0: | ||
Starting items: 92, 73, 86, 83, 65, 51, 55, 93 | ||
Operation: new = old * 5 | ||
Test: divisible by 11 | ||
If true: throw to monkey 3 | ||
If false: throw to monkey 4 | ||
Monkey 1: | ||
Starting items: 99, 67, 62, 61, 59, 98 | ||
Operation: new = old * old | ||
Test: divisible by 2 | ||
If true: throw to monkey 6 | ||
If false: throw to monkey 7 | ||
Monkey 2: | ||
Starting items: 81, 89, 56, 61, 99 | ||
Operation: new = old * 7 | ||
Test: divisible by 5 | ||
If true: throw to monkey 1 | ||
If false: throw to monkey 5 | ||
Monkey 3: | ||
Starting items: 97, 74, 68 | ||
Operation: new = old + 1 | ||
Test: divisible by 17 | ||
If true: throw to monkey 2 | ||
If false: throw to monkey 5 | ||
Monkey 4: | ||
Starting items: 78, 73 | ||
Operation: new = old + 3 | ||
Test: divisible by 19 | ||
If true: throw to monkey 2 | ||
If false: throw to monkey 3 | ||
Monkey 5: | ||
Starting items: 50 | ||
Operation: new = old + 5 | ||
Test: divisible by 7 | ||
If true: throw to monkey 1 | ||
If false: throw to monkey 6 | ||
Monkey 6: | ||
Starting items: 95, 88, 53, 75 | ||
Operation: new = old + 8 | ||
Test: divisible by 3 | ||
If true: throw to monkey 0 | ||
If false: throw to monkey 7 | ||
Monkey 7: | ||
Starting items: 50, 77, 98, 85, 94, 56, 89 | ||
Operation: new = old + 2 | ||
Test: divisible by 13 | ||
If true: throw to monkey 4 | ||
If false: throw to monkey 0""" |
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,58 @@ | ||
import operator as op | ||
import re | ||
|
||
from inputs import REAL as data | ||
|
||
|
||
class Monkey: | ||
PARSE_MONKEY = r"""Monkey \d: | ||
Starting items: (?P<items>[\d, ]+) | ||
Operation: new = old (?P<op>[\+-/\*]) (?P<op_target>\d+|old) | ||
Test: divisible by (?P<test_div_by>\d+) | ||
If true: throw to monkey (?P<test_pass_throw_to>\d) | ||
If false: throw to monkey (?P<test_fail_throw_to>\d)""" | ||
|
||
OPS = {"+": op.add, "-": op.sub, "*": op.mul, "/": op.floordiv} | ||
|
||
def __init__(self, monkey_string, worry_divisor): | ||
fields = re.match(self.PARSE_MONKEY, monkey_string).groupdict() | ||
self.items = list(map(int, fields['items'].split(", "))) | ||
self.op = self.OPS[fields["op"]] | ||
self.op_target = int(fields["op_target"]) if fields["op_target"] != "old" else None # None means use "old" as a target | ||
self.worry_divisor = worry_divisor | ||
self.test_div_by = int(fields["test_div_by"]) | ||
self.test_pass_throw_to = int(fields["test_pass_throw_to"]) | ||
self.test_fail_throw_to = int(fields["test_fail_throw_to"]) | ||
self.item_inspection_counter = 0 | ||
|
||
def turn(self, monkeys): | ||
while self.items: | ||
self.item_inspection_counter += 1 | ||
item = self.items.pop(0) | ||
item = self.op(item, self.op_target if self.op_target else item) # worry operation | ||
item = item // self.worry_divisor # boredom | ||
next_monkey = self.test_pass_throw_to if item % self.test_div_by == 0 else self.test_fail_throw_to | ||
monkeys[next_monkey].items.append(item) | ||
|
||
def __repr__(self): | ||
return f"{self.items} {self.op} {self.op_target} {self.test_div_by} {self.test_pass_throw_to} {self.test_fail_throw_to}" | ||
|
||
|
||
|
||
def simulate_monkey_inspection_counts(worry_divisor, round_count): | ||
monkeys = {i: Monkey(m, worry_divisor) for i, m in enumerate(data.split("\n\n"))} | ||
|
||
for round in range(round_count): | ||
print(round) | ||
for i in range(len(monkeys)): | ||
monkeys[i].turn(monkeys) | ||
|
||
# print(f"After round {round+1}, the monkeys are holding items with these worry levels:") | ||
# for i in range(len(monkeys)): | ||
# print(f"Monkey {i}: {monkeys[i].items}") | ||
|
||
return sorted(m.item_inspection_counter for m in monkeys.values()) | ||
|
||
|
||
print(f"Part 1: {(counts := simulate_monkey_inspection_counts(3, 20))[-1] * counts[-2]}") | ||
print(f"Part 2: {(counts := simulate_monkey_inspection_counts(1, 10000))[-1] * counts[-2]}") |