From aeda596ba76e127c9cb2d48d2adc03e62466f302 Mon Sep 17 00:00:00 2001 From: Stefan Nygaard Hansen Date: Wed, 20 Dec 2023 11:23:54 +0100 Subject: [PATCH] Add day20, 2023 --- 2023/day20/input | 58 +++++++++++++++++++++++++++++++++ 2023/day20/solution.py | 74 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 2023/day20/input create mode 100644 2023/day20/solution.py diff --git a/2023/day20/input b/2023/day20/input new file mode 100644 index 0000000..ff3f47e --- /dev/null +++ b/2023/day20/input @@ -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 diff --git a/2023/day20/solution.py b/2023/day20/solution.py new file mode 100644 index 0000000..651878f --- /dev/null +++ b/2023/day20/solution.py @@ -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)) \ No newline at end of file