day11 part2
This commit is contained in:
parent
7874d21e18
commit
695b77fef7
@ -1,4 +1,5 @@
|
|||||||
import shared
|
import shared
|
||||||
|
import math
|
||||||
from scanf import scanf
|
from scanf import scanf
|
||||||
import matrix
|
import matrix
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
@ -18,7 +19,6 @@ class Monkey:
|
|||||||
inspect_count: int = 0
|
inspect_count: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def load_monkeys(lines):
|
def load_monkeys(lines):
|
||||||
monkeys = []
|
monkeys = []
|
||||||
count = 0
|
count = 0
|
||||||
@ -52,14 +52,13 @@ def load_monkeys(lines):
|
|||||||
)
|
)
|
||||||
monkeys.append(monkey)
|
monkeys.append(monkey)
|
||||||
count+=1
|
count+=1
|
||||||
return monkeys
|
lcm = math.lcm(*[m.div for m in monkeys])
|
||||||
|
return monkeys, lcm
|
||||||
|
|
||||||
def part1(lines):
|
def part1(lines):
|
||||||
monkeys = load_monkeys(lines)
|
monkeys, _ = load_monkeys(lines)
|
||||||
for monkey in monkeys:
|
|
||||||
print(monkey)
|
|
||||||
|
|
||||||
for current_round in range(20): # TODO: WHILE
|
for current_round in range(20):
|
||||||
for monkey in monkeys:
|
for monkey in monkeys:
|
||||||
for item in monkey.items:
|
for item in monkey.items:
|
||||||
monkey.inspect_count += 1
|
monkey.inspect_count += 1
|
||||||
@ -68,9 +67,7 @@ def part1(lines):
|
|||||||
item = monkey.op(item, item)
|
item = monkey.op(item, item)
|
||||||
else:
|
else:
|
||||||
item = monkey.op(item, monkey.by)
|
item = monkey.op(item, monkey.by)
|
||||||
print(item,monkey.by, monkey.op)
|
|
||||||
item = item // 3
|
item = item // 3
|
||||||
print(item)
|
|
||||||
if item % monkey.div == 0:
|
if item % monkey.div == 0:
|
||||||
to = monkey.t
|
to = monkey.t
|
||||||
else:
|
else:
|
||||||
@ -85,15 +82,12 @@ def part1(lines):
|
|||||||
print(total)
|
print(total)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def part2(lines):
|
def part2(lines):
|
||||||
monkeys = load_monkeys(lines)
|
monkeys, lcm = load_monkeys(lines)
|
||||||
for monkey in monkeys:
|
|
||||||
print(monkey)
|
|
||||||
|
|
||||||
for current_round in range(10000): # TODO: WHILE
|
for current_round in range(10000):
|
||||||
for monkey in monkeys:
|
for monkey in monkeys:
|
||||||
|
print(monkey)
|
||||||
for item in monkey.items:
|
for item in monkey.items:
|
||||||
monkey.inspect_count += 1
|
monkey.inspect_count += 1
|
||||||
if monkey.by is None:
|
if monkey.by is None:
|
||||||
@ -101,13 +95,13 @@ def part2(lines):
|
|||||||
item = monkey.op(item, item)
|
item = monkey.op(item, item)
|
||||||
else:
|
else:
|
||||||
item = monkey.op(item, monkey.by)
|
item = monkey.op(item, monkey.by)
|
||||||
|
item = item % lcm
|
||||||
if item % monkey.div == 0:
|
if item % monkey.div == 0:
|
||||||
to = monkey.t
|
to = monkey.t
|
||||||
else:
|
else:
|
||||||
to = monkey.f
|
to = monkey.f
|
||||||
monkeys[to].items.append(item)
|
monkeys[to].items.append(item)
|
||||||
monkey.items = []
|
monkey.items = []
|
||||||
print("-----current round", current_round)
|
|
||||||
|
|
||||||
counts = list(sorted([monkey.inspect_count for monkey in monkeys]))
|
counts = list(sorted([monkey.inspect_count for monkey in monkeys]))
|
||||||
print(counts)
|
print(counts)
|
||||||
|
Loading…
Reference in New Issue
Block a user