blob: 37347c7d5cdac5bdfa8e89792e564e9d825e461b (
plain) (
blame)
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
|
#!/usr/bin/env python3
# START PART 2
from copy import deepcopy
# END PART 2
from numpy import uint16
recipes: dict[str, list[str] | uint16] = {}
def solve(wire: str) -> uint16:
if wire.isdigit():
return int(wire)
if type(recipes[wire]) == uint16:
return recipes[wire]
match recipes[wire]:
case [x]:
recipes[wire] = solve(x)
case ["NOT", x]:
recipes[wire] = ~solve(x)
case [x, "AND", y]:
recipes[wire] = solve(x) & solve(y)
case [x, "OR", y]:
recipes[wire] = solve(x) | solve(y)
case [x, "LSHIFT", y]:
recipes[wire] = solve(x) << solve(y)
case [x, "RSHIFT", y]:
recipes[wire] = solve(x) >> solve(y)
return recipes[wire]
def main() -> None:
global recipes
with open("input", "r", encoding="utf-8") as f:
for line in f.readlines():
line = line.strip().split(" ")
recipes[line[-1]] = line[:-2]
# START PART 2
backup = deepcopy(recipes)
backup["b"] = solve("a")
recipes = backup
# END PART 2
print(solve("a"))
if __name__ == "__main__":
main()
|