Solve 2024:24 pt1 "Crossed Wires"
No idea how to solve pt2, so it will rest for now.
This commit is contained in:
parent
1b7b30e296
commit
72574e753f
1 changed files with 42 additions and 0 deletions
42
2024-python/output/day_24.py
Normal file
42
2024-python/output/day_24.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
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()]
|
||||||
|
while Q:
|
||||||
|
a, o, b, _, c = Q.pop(0)
|
||||||
|
if a not in values or b not in values:
|
||||||
|
Q.append((a, o, b, "_", c))
|
||||||
|
continue
|
||||||
|
a, b = int(values[a]), int(values[b])
|
||||||
|
match o:
|
||||||
|
case "AND":
|
||||||
|
values[c] = int(a + b == 2)
|
||||||
|
case "XOR":
|
||||||
|
values[c] = int(a + b == 1)
|
||||||
|
case "OR":
|
||||||
|
values[c] = int(a + b > 0)
|
||||||
|
p1 = int(
|
||||||
|
"".join(
|
||||||
|
[
|
||||||
|
str(v)
|
||||||
|
for k, v in sorted(values.items(), key=lambda x: x[0], reverse=True)
|
||||||
|
if k.startswith("z")
|
||||||
|
]
|
||||||
|
),
|
||||||
|
2,
|
||||||
|
)
|
||||||
|
p2 = None
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/24.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
|
|
||||||
|
assert p1 == 36035961805936
|
||||||
|
# assert p2 == 0
|
||||||
Loading…
Add table
Reference in a new issue