-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.py
50 lines (31 loc) · 1.01 KB
/
07.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
from lib import *
input = read_input(2015, 7)
MOD = 1 << 16
funcs = {dest: args for *args, _, dest in map(str.split, input.splitlines())}
dp = {}
def solve(key):
if key not in dp:
if key.isnumeric():
val = int(key)
else:
args = funcs[key]
if len(args) == 1:
val = solve(args[0])
elif len(args) == 2:
if args[0] == "NOT":
val = ~solve(args[1]) % MOD
elif len(args) == 3:
if args[1] == "OR":
val = solve(args[0]) | solve(args[2])
elif args[1] == "AND":
val = solve(args[0]) & solve(args[2])
elif args[1] == "LSHIFT":
val = solve(args[0]) << solve(args[2])
elif args[1] == "RSHIFT":
val = solve(args[0]) >> solve(args[2])
dp[key] = val % MOD
return dp[key]
print(solve("a"))
dp.clear()
dp = {"b": solve("a")}
print(solve("a"))