[Python/2015] Move solutions into .py files
This commit is contained in:
parent
8c2be5fb77
commit
94dd3ae399
52 changed files with 996 additions and 3314 deletions
50
Python/2015/07.py
Normal file
50
Python/2015/07.py
Normal file
|
@ -0,0 +1,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"))
|
Loading…
Add table
Add a link
Reference in a new issue