-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.b.py
More file actions
71 lines (62 loc) · 2.03 KB
/
07.b.py
File metadata and controls
71 lines (62 loc) · 2.03 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
with open("2015/07.input.txt", encoding="utf-8") as file:
data = file.read()
lines = data.splitlines()
lines = list(filter(lambda x: len(x) > 0, lines))
wires: dict[str, tuple[str, str, str] | str | int] = {}
for line in lines:
parts = line.split()
if len(parts) == 3:
wires[parts[2]] = int(parts[0]) if parts[0].isnumeric() else parts[0]
elif len(parts) == 4:
wires[parts[3]] = (
"",
parts[0],
int(parts[1]) if parts[1].isnumeric() else parts[1],
)
elif len(parts) == 5:
wires[parts[4]] = (
int(parts[0]) if parts[0].isnumeric() else parts[0],
parts[1],
int(parts[2]) if parts[2].isnumeric() else parts[2],
)
def get_value(wire: str | int) -> int:
if isinstance(wire, int):
return wire
if isinstance(wires[wire], int):
return wires[wire]
if isinstance(wires[wire], str):
wires[wire] = get_value(wires[wire])
return wires[wire]
a, op, b = wires[wire]
if op == "NOT":
wires[wire] = ~get_value(b)
elif op == "AND":
wires[wire] = get_value(a) & get_value(b)
elif op == "OR":
wires[wire] = get_value(a) | get_value(b)
elif op == "LSHIFT":
wires[wire] = get_value(a) << int(b)
elif op == "RSHIFT":
wires[wire] = get_value(a) >> int(b)
wires[wire] = wires[wire] & 0xFFFF
return wires[wire]
a = get_value("a")
wires: dict[str, tuple[str, str, str] | str | int] = {}
for line in lines:
parts = line.split()
if len(parts) == 3:
wires[parts[2]] = int(parts[0]) if parts[0].isnumeric() else parts[0]
elif len(parts) == 4:
wires[parts[3]] = (
"",
parts[0],
int(parts[1]) if parts[1].isnumeric() else parts[1],
)
elif len(parts) == 5:
wires[parts[4]] = (
int(parts[0]) if parts[0].isnumeric() else parts[0],
parts[1],
int(parts[2]) if parts[2].isnumeric() else parts[2],
)
wires["b"] = a
print(get_value("a"))