From fcbeb4bfdd9438b22439a4c9cc8fb3e1965942d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Sat, 4 Jan 2025 23:26:27 +0100 Subject: [PATCH] Solve 2024:24 pt2 "Code Chronicle" To solve this, it was easier to visualize the graph and spot errors. This code is kept for reference. To learn the mechanics, These subreddit threads are helpful: - https://www.reddit.com/r/adventofcode/comments/1hla5ql/2024_day_24_part_2_a_guide_on_the_idea_behind_the/ - https://www.reddit.com/r/adventofcode/comments/1hneuf0/2024_day_24_part_2_finally_solved_it/ --- 2024-python/output/day_24.py | 40 +++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/2024-python/output/day_24.py b/2024-python/output/day_24.py index 3474478..a4b7d97 100644 --- a/2024-python/output/day_24.py +++ b/2024-python/output/day_24.py @@ -1,7 +1,12 @@ +import graphviz +import re + + def solve(data): values, wires = data.split("\n\n") values = dict([tuple(c.strip().split(": ")) for c in values.splitlines()]) - Q = [w.split() for w in wires.splitlines()] + wires = [w.split() for w in wires.splitlines()] + Q = [*wires] while Q: a, o, b, _, c = Q.pop(0) if a not in values or b not in values: @@ -25,10 +30,38 @@ def solve(data): ), 2, ) - p2 = None + # render_graph(wires) + wrong_xors_inputs = r"[^y|x]\S{2}+ XOR [^x|y]\S{2}+ -> ([^z]\S{2})" + wrong_z_outputs = r"\s(?:OR|AND) .+ -> (z\d{2})" + found_manually = ["jqf", "skh"] + p2 = ",".join( + sorted( + [seq for seq in re.findall(wrong_z_outputs, data) if not seq.endswith("45")] + + list(re.findall(wrong_xors_inputs, data)) + + found_manually + ) + ) return p1, p2 +def render_graph(wires): + dot = graphviz.Digraph(comment="The Round Table") + + for a, o, b, _, c in wires: + ol = f"{a}{b}{o}{c}" + dot.node(a) + dot.node(b) + dot.node(c) + + dot.node(ol, label=o, shape="diamond") + + dot.edge(a, ol) + dot.edge(b, ol) + dot.edge(ol, c) + + dot.render("2024.24.p2.gv").replace("\\", "/") + + if __name__ == "__main__": with open("./input/24.txt", "r") as f: inp = f.read().strip() @@ -37,6 +70,3 @@ if __name__ == "__main__": print(p1) print(p2) - - assert p1 == 36035961805936 - # assert p2 == 0