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/
This commit is contained in:
parent
d7d5312786
commit
fcbeb4bfdd
1 changed files with 35 additions and 5 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue