Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit 6c4167b

Browse files
authored
Solve part 1
1 parent d5b696f commit 6c4167b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Day-24/python/paul2708/day24.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import Tuple, List, Dict
2+
3+
from tqdm import tqdm
4+
5+
from shared.paul2708.input_reader import *
6+
from shared.paul2708.output import write
7+
8+
lines = read_plain_input(day=24, example=None)
9+
initialization = [(line.split(": ")[0], int(line.split(": ")[1])) for line in lines[:lines.index("")]]
10+
gates = [(line.split(" ")[1], line.split(" ")[0], line.split(" ")[2], line.split(" ")[4]) for line in lines[lines.index("") + 1:]]
11+
12+
wires = set([a for a, _ in initialization])
13+
wires.update([a for _, a, _, _ in gates])
14+
wires.update([b for _, _, b, _ in gates])
15+
wires.update([c for _, _, _, c in gates])
16+
17+
print(initialization)
18+
print(gates)
19+
print(wires)
20+
21+
known_outputs = dict()
22+
for wire, val in initialization:
23+
known_outputs[wire] = val
24+
25+
def is_one(wire):
26+
return wire in known_outputs and known_outputs[wire] == 1
27+
28+
def is_zero(wire):
29+
return wire in known_outputs and known_outputs[wire] == 0
30+
31+
32+
while len(known_outputs) != len(wires):
33+
for operation, op1, op2, output in gates:
34+
if operation == "AND" and (is_zero(op1) or is_zero(op2)):
35+
known_outputs[output] = 0
36+
elif operation == "AND" and (is_one(op1) and is_one(op2)):
37+
known_outputs[output] = 1
38+
if operation == "OR" and (is_one(op1) or is_one(op2)):
39+
known_outputs[output] = 1
40+
elif operation == "OR" and (is_zero(op1) and is_zero(op2)):
41+
known_outputs[output] = 0
42+
43+
if operation == "XOR" and op1 in known_outputs and op2 in known_outputs:
44+
known_outputs[output] = known_outputs[op1] ^ known_outputs[op2]
45+
46+
indices = []
47+
for a in known_outputs:
48+
if a.startswith("z"):
49+
indices.append(a)
50+
51+
indices = sorted(indices, reverse=True)
52+
print(indices)
53+
54+
print(known_outputs)
55+
s = ""
56+
for a in indices:
57+
s += str(known_outputs[a])
58+
59+
print(s)
60+
61+
print(int(s, 2))

0 commit comments

Comments
 (0)