-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_1.py
52 lines (42 loc) · 1.43 KB
/
part_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import re
from typing import Dict, List
def parseInput() -> List[List[str]]:
with open("input.txt", "r") as f:
return [part.split("\n") for part in f.read().strip().split("\n\n")]
def simulateGates(data):
state: Dict[str, int] = {}
for wire in data[0]:
name, value = wire.split(": ")
state[name] = int(value)
loop = True
while loop:
should_loop_again = False
for gate in data[1]:
match = re.match(r"^(.*) (AND|OR|XOR) (.*) -> (.*)$", gate)
if match:
left, operator, right, output = match.groups()
if left not in state or right not in state:
should_loop_again = True
continue
if operator == "AND":
state[output] = state[left] & state[right]
elif operator == "OR":
state[output] = state[left] | state[right]
elif operator == "XOR":
state[output] = state[left] ^ state[right]
loop = should_loop_again
return state
# Part -1
def decimalWireZ() -> int:
data = parseInput()
state = simulateGates(data)
bits = "".join(
str(state[name])
for name, _ in sorted(
filter(lambda x: x[0].startswith("z"), state.items()),
key=lambda x: x[0],
reverse=True,
)
)
return int(bits, 2)
# print("Part 1", decimalWireZ())