2022 day 11
This commit is contained in:
55
2022/11/input
Normal file
55
2022/11/input
Normal file
@@ -0,0 +1,55 @@
|
||||
Monkey 0:
|
||||
Starting items: 89, 73, 66, 57, 64, 80
|
||||
Operation: new = old * 3
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 6
|
||||
If false: throw to monkey 2
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 83, 78, 81, 55, 81, 59, 69
|
||||
Operation: new = old + 1
|
||||
Test: divisible by 3
|
||||
If true: throw to monkey 7
|
||||
If false: throw to monkey 4
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 76, 91, 58, 85
|
||||
Operation: new = old * 13
|
||||
Test: divisible by 7
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 4
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 71, 72, 74, 76, 68
|
||||
Operation: new = old * old
|
||||
Test: divisible by 2
|
||||
If true: throw to monkey 6
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 4:
|
||||
Starting items: 98, 85, 84
|
||||
Operation: new = old + 7
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 5
|
||||
If false: throw to monkey 7
|
||||
|
||||
Monkey 5:
|
||||
Starting items: 78
|
||||
Operation: new = old + 8
|
||||
Test: divisible by 5
|
||||
If true: throw to monkey 3
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 6:
|
||||
Starting items: 86, 70, 60, 88, 88, 78, 74, 83
|
||||
Operation: new = old + 4
|
||||
Test: divisible by 11
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 2
|
||||
|
||||
Monkey 7:
|
||||
Starting items: 81, 58
|
||||
Operation: new = old + 5
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 3
|
||||
If false: throw to monkey 5
|
58
2022/11/part1
Executable file
58
2022/11/part1
Executable file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import re
|
||||
|
||||
MONKEYS = []
|
||||
|
||||
def parse_factor(old, factor):
|
||||
if factor == 'old':
|
||||
return old
|
||||
|
||||
return int(factor)
|
||||
|
||||
OPS = {
|
||||
'*': lambda old, factor: old * parse_factor(old, factor),
|
||||
'+': lambda old, factor: old + parse_factor(old, factor),
|
||||
}
|
||||
|
||||
with open('input') as f:
|
||||
monkey = {'inspections': 0}
|
||||
|
||||
for line in f:
|
||||
if line == "\n":
|
||||
MONKEYS.append(monkey)
|
||||
monkey = {'inspections': 0}
|
||||
elif match := re.match('^Monkey (\d+):$', line):
|
||||
monkey['id'] = int(match[1])
|
||||
elif match := re.match('^ Starting items:', line):
|
||||
_, items = line.rstrip().split(":")
|
||||
monkey['items'] = list(map(int, items.split(", ")))
|
||||
elif match := re.match('^ Operation: new = old ([\*\+]) (\w+)', line):
|
||||
monkey['op_op'] = match[1]
|
||||
monkey['op_factor'] = match[2]
|
||||
elif match := re.match('^ Test: divisible by (\d+)$', line):
|
||||
monkey['test_factor'] = int(match[1])
|
||||
elif match := re.match('^ If (true|false): throw to monkey (\d+)$', line):
|
||||
monkey['test_'+match[1]] = int(match[2])
|
||||
else:
|
||||
raise Exception('unhandled line: '+line)
|
||||
MONKEYS.append(monkey)
|
||||
|
||||
for idx, monkey in enumerate(MONKEYS):
|
||||
assert(idx == monkey['id'])
|
||||
|
||||
for r in range(1, 21): # round
|
||||
for monkey in MONKEYS: # turn
|
||||
for item in monkey['items']:
|
||||
item = OPS[monkey['op_op']](item, monkey['op_factor'])
|
||||
item = item // 3
|
||||
if item % monkey['test_factor'] == 0:
|
||||
MONKEYS[monkey['test_true']]['items'].append(item)
|
||||
else:
|
||||
MONKEYS[monkey['test_false']]['items'].append(item)
|
||||
monkey['inspections'] = monkey['inspections'] + 1
|
||||
monkey['items'] = []
|
||||
|
||||
by_inspections = sorted(MONKEYS, key=lambda monkey: monkey['inspections'])
|
||||
|
||||
print(by_inspections[-1]['inspections'] * by_inspections[-2]['inspections'])
|
62
2022/11/part2
Executable file
62
2022/11/part2
Executable file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import re
|
||||
import math
|
||||
|
||||
MONKEYS = []
|
||||
|
||||
def parse_factor(old, factor):
|
||||
if factor == 'old':
|
||||
return old
|
||||
|
||||
return int(factor)
|
||||
|
||||
OPS = {
|
||||
'*': lambda old, factor: old * parse_factor(old, factor),
|
||||
'+': lambda old, factor: old + parse_factor(old, factor),
|
||||
}
|
||||
|
||||
with open('input') as f:
|
||||
monkey = {'inspections': 0}
|
||||
|
||||
for line in f:
|
||||
if line == "\n":
|
||||
MONKEYS.append(monkey)
|
||||
monkey = {'inspections': 0}
|
||||
elif match := re.match('^Monkey (\d+):$', line):
|
||||
monkey['id'] = int(match[1])
|
||||
elif match := re.match('^ Starting items:', line):
|
||||
_, items = line.rstrip().split(":")
|
||||
monkey['items'] = list(map(int, items.split(", ")))
|
||||
elif match := re.match('^ Operation: new = old ([\*\+]) (\w+)', line):
|
||||
monkey['op_op'] = match[1]
|
||||
monkey['op_factor'] = match[2]
|
||||
elif match := re.match('^ Test: divisible by (\d+)$', line):
|
||||
monkey['test_factor'] = int(match[1])
|
||||
elif match := re.match('^ If (true|false): throw to monkey (\d+)$', line):
|
||||
monkey['test_'+match[1]] = int(match[2])
|
||||
else:
|
||||
raise Exception('unhandled line: '+line)
|
||||
MONKEYS.append(monkey)
|
||||
|
||||
for idx, monkey in enumerate(MONKEYS):
|
||||
assert(idx == monkey['id'])
|
||||
|
||||
# keep the size of the numbers down
|
||||
lcm = math.lcm(*map(lambda monkey: monkey['test_factor'], MONKEYS))
|
||||
|
||||
for r in range(0,10000): # round
|
||||
for monkey in MONKEYS: # turn
|
||||
for item in monkey['items']:
|
||||
item = OPS[monkey['op_op']](item, monkey['op_factor'])
|
||||
item = item % lcm
|
||||
if item % monkey['test_factor'] == 0:
|
||||
MONKEYS[monkey['test_true']]['items'].append(item)
|
||||
else:
|
||||
MONKEYS[monkey['test_false']]['items'].append(item)
|
||||
monkey['inspections'] = monkey['inspections'] + 1
|
||||
monkey['items'] = []
|
||||
|
||||
by_inspections = sorted(MONKEYS, key=lambda monkey: monkey['inspections'])
|
||||
|
||||
print(by_inspections[-1]['inspections'] * by_inspections[-2]['inspections'])
|
@@ -9,7 +9,7 @@ a programming language I'm interested in learning more about.
|
||||
2018, 2019, and 2020 were in their own separate repositories, but have been
|
||||
`git subtree`'d into this one - no point creating a new repository every year.
|
||||
|
||||
* [2022](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2022) 🌟 `20` - Python
|
||||
* [2022](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2022) 🌟 `22` - Python
|
||||
* [2021](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2021) 🌟 ` 5` - Python
|
||||
* [2020](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2020) 🌟 `20` - Javascript
|
||||
* [2019](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2019) 🌟 `22` - Zig
|
||||
@@ -18,6 +18,6 @@ a programming language I'm interested in learning more about.
|
||||
* 2016 🌟 `16` - LISP?
|
||||
* 2015 🌟 ` 9` - Ruby?
|
||||
|
||||
Total: 🌟 `98`
|
||||
Total: 🌟 `100`
|
||||
|
||||
Maybe I'll backfill some of the old years one day.
|
||||
|
Reference in New Issue
Block a user