-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
%sf -> pz, gj | ||
%zh -> bc, st | ||
%hk -> bc | ||
&bc -> mn, zl, xb, mm, dh, hv, gz | ||
%st -> bc, mm | ||
%gv -> xf, qq | ||
%hv -> xb | ||
%nd -> gj, tr | ||
%zx -> bx, ms | ||
%sc -> ks, gj | ||
%gr -> hn | ||
%pl -> qq, rh | ||
%qc -> sf, gj | ||
%xr -> sc, gj | ||
%zl -> zh | ||
&gj -> ks, ld, sg, xr | ||
%dg -> ll, bx | ||
%nf -> bc, tg | ||
%lz -> cv, qq | ||
%nq -> dg, bx | ||
%rh -> qq, lp | ||
%xf -> qq, qj | ||
%ms -> bx, xh | ||
%mn -> bc, hv | ||
&jm -> rx | ||
%xh -> vt, bx | ||
%pz -> gj | ||
%vq -> bt | ||
%gz -> nf | ||
%bt -> gr | ||
&sg -> jm | ||
%fr -> bx, tb | ||
&lm -> jm | ||
%ld -> cl | ||
%cv -> vq | ||
%cl -> gj, jf | ||
%tr -> gj, sz | ||
%sz -> gj, ld | ||
%dx -> hk, bc | ||
%lr -> bx, fr | ||
%vt -> lr, bx | ||
%ll -> zx | ||
broadcaster -> pl, xr, mn, xc | ||
%lp -> lz | ||
%mm -> gz | ||
&qq -> lm, gr, cv, vq, lp, pl, bt | ||
%xb -> zl | ||
&bx -> ll, xc, db | ||
%tb -> bx | ||
%hn -> gv, qq | ||
%jf -> qc, gj | ||
%qj -> qq | ||
%xc -> bx, pm | ||
%tg -> bc, dx | ||
&dh -> jm | ||
%ks -> nd | ||
&db -> jm | ||
%pm -> bx, nq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from math import lcm | ||
from functools import reduce | ||
|
||
with open("input") as f: | ||
inp = f.read().strip().split("\n") | ||
|
||
|
||
type_ = {} | ||
dests = {} | ||
memory = {} | ||
for line in inp: | ||
from_, dest = line.split(" -> ") | ||
typ = from_[0] | ||
name = from_[1:] | ||
if typ == "b": | ||
name = from_ | ||
typ = "" | ||
type_[name] = typ | ||
dests[name] = dest.split(", ") | ||
if typ == "&": | ||
memory[name] = {} | ||
elif typ == "%": | ||
memory[name] = 0 | ||
|
||
for from_, dests_ in dests.items(): | ||
for dest in dests_: | ||
if dest in type_ and type_[dest] == "&": | ||
memory[dest][from_] = 0 | ||
|
||
|
||
cycle, n_low, n_high = 0, 0, 0 | ||
def press_button(): | ||
global cycle | ||
global n_low | ||
global n_high | ||
q = [("button", "broadcaster", 0)] | ||
while q: | ||
from_, to, pulse = q.pop(0) | ||
n_low += 1-pulse | ||
n_high += pulse | ||
if to not in dests: | ||
continue | ||
type_to = type_[to] | ||
dests_to = dests[to] | ||
if type_to == "": | ||
for dest in dests_to: | ||
q.append((to, dest, 0)) | ||
elif type_to == "%": | ||
if pulse == 0: | ||
memory[to] = (memory[to]+1)%2 | ||
for dest in dests_to: | ||
q.append((to, dest, memory[to])) | ||
elif type_to == "&": | ||
memory[to][from_] = pulse | ||
if to == "jm" and pulse == 1: | ||
cycles.append(cycle+1) | ||
if all(x == 1 for x in memory[to].values()): | ||
for dest in dests_to: | ||
q.append((to, dest, 0)) | ||
else: | ||
for dest in dests_to: | ||
q.append((to, dest, 1)) | ||
cycle += 1 | ||
|
||
|
||
# The module "rx" gets a low pulse if and only module "jm" has last been sent high pulses from modules "dh", "lm", "dg" and "db". | ||
# We figure out when each of these send high pulses to "jm" and anticipate a cyclic nature. | ||
cycles = [] | ||
while (len(cycles) < 4): | ||
if cycle == 1000: | ||
print(n_low*n_high) | ||
press_button() | ||
|
||
print(reduce(lcm, cycles)) |