2024-12-05 19:47:18 +01:00
|
|
|
#!/bin/python3
|
|
|
|
import sys,re
|
|
|
|
from pprint import pprint
|
|
|
|
sys.path.insert(0, '../../')
|
|
|
|
from fred import list2int,get_re,nprint,lprint,loadFile,convert_list
|
|
|
|
|
|
|
|
input_f = 'input'
|
|
|
|
|
|
|
|
part = 1
|
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 1 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
|
|
|
|
# need to use recursion. Should backtrack to a value when it has been lit
|
|
|
|
|
|
|
|
def parse(lst:str):
|
|
|
|
if 'AND' in lst or 'OR' in lst or 'SHIFT' in lst:
|
|
|
|
t = get_re(r"^(.+) (AND|OR|[L|R]SHIFT) (.+) -> (.+)",lst)
|
|
|
|
return convert_list([t.group(2), t.group(1),t.group(3),t.group(4)])
|
|
|
|
|
|
|
|
elif 'NOT' in lst:
|
|
|
|
t = get_re(r"^NOT (.+) -> (.+)",lst)
|
|
|
|
return convert_list(['NOT',t.group(1),t.group(2)])
|
|
|
|
else:
|
|
|
|
t = get_re(r"^(.+) -> (.+)",lst)
|
|
|
|
return convert_list(['SET',t.group(1),t.group(2)])
|
|
|
|
|
2024-12-05 23:13:04 +01:00
|
|
|
def checkValue(x,values):
|
|
|
|
if x not in values and not isinstance(x, int):
|
|
|
|
return x
|
|
|
|
|
2024-12-05 19:47:18 +01:00
|
|
|
def v_return(x,values):
|
|
|
|
if isinstance(x, str):
|
|
|
|
if x not in values:
|
2024-12-05 23:13:04 +01:00
|
|
|
return # the values is not 0 if it hasn't been used.
|
2024-12-05 19:47:18 +01:00
|
|
|
else:
|
|
|
|
return values[x]
|
|
|
|
return x
|
|
|
|
|
|
|
|
if part == 1:
|
2024-12-05 23:13:04 +01:00
|
|
|
missing = []
|
2024-12-05 19:47:18 +01:00
|
|
|
instructions = loadFile(input_f)
|
|
|
|
values = {}
|
2024-12-05 23:13:04 +01:00
|
|
|
|
|
|
|
def parseValues(t):
|
2024-12-05 19:47:18 +01:00
|
|
|
if t[0] == 'SET':
|
|
|
|
values[t[2]] = v_return(t[1],values)
|
|
|
|
elif t[0] == 'AND':
|
|
|
|
values[t[3]] = v_return(t[1],values) & v_return(t[2],values)
|
|
|
|
elif t[0] == 'OR':
|
|
|
|
values[t[3]] = v_return(t[1],values) | v_return(t[2],values)
|
|
|
|
elif t[0] == 'LSHIFT':
|
|
|
|
values[t[3]] = v_return(t[1],values) << v_return(t[2],values)
|
|
|
|
elif t[0] == 'RSHIFT':
|
|
|
|
values[t[3]] = v_return(t[1],values) >> v_return(t[2],values)
|
|
|
|
elif t[0] == 'NOT':
|
|
|
|
values[t[2]] = 65535-v_return(t[1],values)
|
2024-12-05 23:13:04 +01:00
|
|
|
|
|
|
|
for i in instructions:
|
|
|
|
t = parse(i)
|
|
|
|
print(t)
|
|
|
|
if isinstance(t[1], str) or isinstance(t[2], str):
|
|
|
|
if checkValue(t[1],values) is not None or checkValue(t[2],values) is not None:
|
|
|
|
missing.append(t)
|
|
|
|
else:
|
|
|
|
parseValues(t)
|
|
|
|
print(values)
|
|
|
|
print(missing)
|
|
|
|
#input()
|
2024-12-05 19:47:18 +01:00
|
|
|
pprint(values['a'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 2 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
if part == 2:
|
|
|
|
exit()
|