Update readme and clean up day 20.

This commit is contained in:
2023-12-27 13:26:40 -05:00
parent c5813e168b
commit 9fcc902703
2 changed files with 22 additions and 12 deletions

16
d20.py
View File

@@ -1,5 +1,6 @@
from lib import *
from math import lcm
from collections import deque
EXAMPLE = """
broadcaster -> a, b, c
@@ -25,10 +26,9 @@ def visualize(modules):
f.write(" " + m[1] + ' -> ' + cm + "\n")
f.write("}")
def solve(i: Input, second=False):
res = 0
def solve(input: Input, second=False):
modules = {}
for line in i.lines():
for line in input.lines():
if not line: continue
src, dsts = line.split(" -> ")
dsts = dsts.split(", ")
@@ -48,17 +48,17 @@ def solve(i: Input, second=False):
if second:
# visualize(modules)
periods = {d: [] for d in modules["kh"][2].keys()}
(feed,) = [m[1] for m in modules.values() if "rx" in m[3]]
periods = {d: [] for d in modules[feed][2].keys()}
BUTTON_PUSHES = 10000
else:
BUTTON_PUSHES = 1000
lo, hi = 0, 0
for i in range(BUTTON_PUSHES):
qs = [("button module", "broadcaster", 0)]
qs = deque([("button module", "broadcaster", 0)])
while qs:
src, dst, sig = qs[0]
qs = qs[1:]
src, dst, sig = qs.popleft()
if sig == 0:
lo += 1
@@ -87,7 +87,7 @@ def solve(i: Input, second=False):
if new_pulse is not None:
for nxtdst in m[3]:
if second and nxtdst == "kh" and new_pulse == 1:
if second and nxtdst == feed and new_pulse == 1:
# print(f"{i:>4}: {dst[:4]:>4} -{new_pulse}> {nxtdst}")
periods[dst].append(i)
qs.append((dst, nxtdst, new_pulse))