Skip to content

Commit 9397c69

Browse files
committed
Day 11
1 parent 4221735 commit 9397c69

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

2022/day11/input

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

2022/day11/solution.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# with open('test-input') as t:
2+
with open('input') as t:
3+
data = t.read()
4+
5+
def operation(value, operation):
6+
if '*' in operation:
7+
if operation.split(' ')[0] == 'old' and operation.split(' ')[-1] == 'old':
8+
# result = value*value
9+
result = value**2
10+
else:
11+
result = value*int(operation.split(' ')[-1])
12+
elif '+' in operation:
13+
result = value+int(operation.split(' ')[-1])
14+
return int(str(result/3).split('.')[0])
15+
16+
def zero_list(length):
17+
list = []
18+
for x in range(0, length):
19+
list.append(0)
20+
return list
21+
22+
monkeys = []
23+
for monkey in data.split('\n\n'):
24+
monkeys.append(monkey.split('\n'))
25+
26+
for monkey in monkeys:
27+
monkey.pop(0)
28+
monkey[0] = [int(l) for l in (monkey[0].split(':')[-1].split(','))]
29+
monkey[1] = monkey[1].split('=')[-1][1:]
30+
monkey[2] = int(monkey[2].split(' ')[-1])
31+
monkey[3] = int(monkey[3].split(' ')[-1])
32+
monkey[4] = int(monkey[4].split(' ')[-1])
33+
34+
rounds = 20
35+
inspections = zero_list(len(monkeys))
36+
for r in range(0, rounds):
37+
i = 0
38+
for monkey in monkeys:
39+
for item in monkey[0]:
40+
worry = operation(item, monkey[1])
41+
if worry%monkey[2] == 0:
42+
monkeys[monkey[3]][0].append(worry)
43+
else:
44+
monkeys[monkey[4]][0].append(worry)
45+
inspections[i] += len(monkey[0])
46+
monkey[0] = []
47+
i += 1
48+
49+
inspections.sort()
50+
business = inspections[-1]*inspections[-2]
51+
print(business)

2022/day11/test-part2.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import sys
2+
3+
# with open('test-input') as t:
4+
with open('input') as t:
5+
data = t.read()
6+
7+
# sys.set_int_max_str_digits(1000000)
8+
9+
def operation(value, operation):
10+
if '*' in operation:
11+
if operation.split(' ')[0] == 'old' and operation.split(' ')[-1] == 'old':
12+
# result = value*value
13+
result = value**2
14+
else:
15+
result = value*int(operation.split(' ')[-1])
16+
elif '+' in operation:
17+
result = value+int(operation.split(' ')[-1])
18+
return result
19+
20+
def zero_list(length):
21+
list = []
22+
for x in range(0, length):
23+
list.append(0)
24+
return list
25+
26+
monkeys = []
27+
for monkey in data.split('\n\n'):
28+
monkeys.append(monkey.split('\n'))
29+
30+
for monkey in monkeys:
31+
monkey.pop(0)
32+
monkey[0] = [int(l) for l in (monkey[0].split(':')[-1].split(','))]
33+
monkey[1] = monkey[1].split('=')[-1][1:]
34+
monkey[2] = int(monkey[2].split(' ')[-1])
35+
monkey[3] = int(monkey[3].split(' ')[-1])
36+
monkey[4] = int(monkey[4].split(' ')[-1])
37+
38+
rounds = 10000
39+
inspections = zero_list(len(monkeys))
40+
for r in range(0, rounds):
41+
i = 0
42+
for monkey in monkeys:
43+
for item in monkey[0]:
44+
worry = operation(item, monkey[1])
45+
if worry%monkey[2] == 0:
46+
monkeys[monkey[3]][0].append(worry)
47+
else:
48+
monkeys[monkey[4]][0].append(worry)
49+
inspections[i] += len(monkey[0])
50+
monkey[0] = []
51+
i += 1
52+
53+
inspections.sort()
54+
business = inspections[-1]*inspections[-2]
55+
print(business)

2022/day11/test.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def list_of_lists(length):
1717
for x in range(0, len(monkeys)):
1818
inspections.append(0)
1919
for r in range(0, rounds):
20+
print('ROUND ', r)
2021
mks = list_of_lists(len(monkeys))
22+
print(monkeys)
2123
for monkey in monkeys:
2224
m = monkey[2].split(' ')
2325
inspections[int(monkey[0].split(' ')[-1].strip(':'))] += len(monkey[1].split(':')[1].split(', '))

0 commit comments

Comments
 (0)