Skip to content

Commit

Permalink
Solution for part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
VitoMinheere committed Dec 11, 2022
1 parent c48bcf7 commit 7cdd0fc
Showing 1 changed file with 51 additions and 14 deletions.
65 changes: 51 additions & 14 deletions 2022/d11/solution.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from math import prod

class Monkey:

Expand Down Expand Up @@ -27,10 +28,21 @@ def set_test_false(self, monkey):
self.test_false = int(monkey)

def operation(self, worry_level: int):
return eval(f"{worry_level} {self.arith}")
self.items_inspected += 1
if "old" in self.arith:
arith = self.arith.replace("old", str(worry_level))
else:
arith = self.arith
return eval(f"{worry_level} {arith}")

def get_next_monkey(self, worry_level):
res = eval(f"{worry_level} {self.test} == 0")
return (self.test_false, self.test_true)[res]

def p1(data):


def p1(data, rounds):
rounds = rounds
worry_level = 0
am_monkeys = int(data[-6].split(" ")[-1][:1])+1

Expand All @@ -44,10 +56,9 @@ def p1(data):
items = [int(x) for x in line.split(":")[-1].split(",")]
monkeys[m].append_items(items)
elif "Operation" in line:
print(line.split("=")[-1][4:])
monkeys[m].set_arithmetic("".join(line.split("=")[-1][4:]).replace("old", str(worry_level)))
monkeys[m].set_arithmetic("".join(line.split("=")[-1][4:]))
elif "Test" in line:
monkeys[m].set_test(f"/ {line.split(' ')[-1]}")
monkeys[m].set_test(f"% {line.split(' ')[-1]}")
elif "true" in line:
monkeys[m].set_test_true(line.split(" ")[-1])
elif "false" in line:
Expand All @@ -56,19 +67,45 @@ def p1(data):
start += 7
end += 7

for monkey in monkeys:
print(monkey.__dict__)

def p2(data):
pass

with open("test.txt") as t:
mod = prod([int(x.test.split(" ")[-1]) for x in monkeys])

for r in range(1, rounds+1):
for i, m in enumerate(monkeys):
# print("Monkey " + str(i))
for _, item in enumerate(m.items):
worry_level = item
# print(f"Monkey inspects an item with a worry level of {item}")
worry_level = m.operation(item)
# print(f"Worry level = " + str(worry_level))
if rounds == 20:
worry_level = worry_level // 3
else:
worry_level = worry_level % mod
# print(f"Monkey gets bored with item. Worry level is divided by 3 to {worry_level}.")
next = m.get_next_monkey(worry_level)
# print(f"Item with worry level {worry_level} is thrown to monkey {str(next)}.")
monkeys[next].append_items([worry_level])
m.items = []

active = sorted(monkeys, key=lambda x: x.items_inspected, reverse=True)
for i, x in enumerate(active):
print(f"Monkey {str(i)} inspected items {str(x.items_inspected)} times.")

p1 = 1
for x in active[:2]:
print(x.items_inspected)
p1 *= x.items_inspected

print("P1 = " + str(p1))


with open("input.txt") as t:
data = t.read().splitlines()
start = time.time()
p1(data)
p1(data, 20)
p1_time = time.time() - start
p2_start = time.time()
p2(data)
p1(data, 10000)
p2_time = time.time() - p2_start

print("P1 took " + str(round(p1_time * 1000)) + " ms")
Expand Down

0 comments on commit 7cdd0fc

Please sign in to comment.