diff options
author | Marvin Borner | 2023-12-20 15:37:19 +0100 |
---|---|---|
committer | Marvin Borner | 2023-12-20 15:43:44 +0100 |
commit | b812ead13b15bb742159f77ba1b89d416cb41c50 (patch) | |
tree | 965ee43f1435540023e4e0fa3ae764f4944af2f3 /2023/20 | |
parent | 05c842e23abe76e33e23fb4a0f1d71fcdf3d1db6 (diff) |
(s, eeelefant)
Diffstat (limited to '2023/20')
-rw-r--r-- | 2023/20/solve.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/2023/20/solve.py b/2023/20/solve.py new file mode 100644 index 0000000..c474654 --- /dev/null +++ b/2023/20/solve.py @@ -0,0 +1,52 @@ +L = [ + (m := l.strip().split(" -> ")) and (m[0][1:], (m[0][0], m[1].split(", "))) + for l in open("input").readlines() +] +L = dict(L) + +state = {} + +# init conjunctions to low pulse +for s, (_, ds) in L.items(): + for d in ds: + k = ("&", d) + if k not in state: + state[k] = {} + state[k][s] = False + +rx = {k: 0 for k in state[("&", list(state[("&", "rx")].keys())[0])]} + +for i in range(1, 424242): + q = [("seeelefant", "roadcaster", False)] + + while q: + s, m, p = q.pop(0) + state[p] = state.get(p, 0) + 1 + + if m not in L: + continue + + k, ds = L[m] + if k == "%": + if p: + continue + else: + state[(k, m)] = not state.get((k, m), False) + p = state[(k, m)] + elif k == "&": + state[(k, m)][s] = p + p = any(not x for x in state[(k, m)].values()) + + if s in rx.keys(): + for _k in state[(k, m)].keys(): + if state[(k, m)][_k]: + rx[_k] = i + + for d in ds: + q.append((m, d, p)) + + if i == 1000: # part 1 + print(state[True] * state[False]) + if all(rx.values()): # part 2 + print(eval("*".join(str(n) for n in rx.values()))) + break |