Working on 2015/07
This commit is contained in:
parent
f4c442bf1a
commit
88b01fc9bd
68
2015/07/solution.py
Normal file
68
2015/07/solution.py
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#!/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)])
|
||||||
|
|
||||||
|
def v_return(x,values):
|
||||||
|
if isinstance(x, str):
|
||||||
|
if x not in values:
|
||||||
|
return 0 # the values is not 0 if it hasn't been used.
|
||||||
|
else:
|
||||||
|
return values[x]
|
||||||
|
return x
|
||||||
|
|
||||||
|
if part == 1:
|
||||||
|
instructions = loadFile(input_f)
|
||||||
|
values = {}
|
||||||
|
for i in instructions:
|
||||||
|
t = parse(i)
|
||||||
|
#print(i)
|
||||||
|
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)
|
||||||
|
#pprint(values)
|
||||||
|
#input()
|
||||||
|
pprint(values['a'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
# #
|
||||||
|
# Part 2 #
|
||||||
|
# #
|
||||||
|
#########################################
|
||||||
|
if part == 2:
|
||||||
|
exit()
|
Binary file not shown.
26
fred.py
26
fred.py
@ -1,5 +1,31 @@
|
|||||||
import re,sys
|
import re,sys
|
||||||
|
|
||||||
|
def loadFile(input_f):
|
||||||
|
lines = []
|
||||||
|
with open(input_f) as file:
|
||||||
|
for line in file:
|
||||||
|
lines.append(line.rstrip())
|
||||||
|
return lines
|
||||||
|
|
||||||
|
def convert_list(input_list):
|
||||||
|
"""
|
||||||
|
Convert a list of strings to integers where possible, leaving others as strings.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
input_list (list): A list of strings to be converted.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: A list with integers or strings based on the input.
|
||||||
|
"""
|
||||||
|
converted_list = []
|
||||||
|
for item in input_list:
|
||||||
|
try:
|
||||||
|
converted_list.append(int(item))
|
||||||
|
except ValueError:
|
||||||
|
converted_list.append(item)
|
||||||
|
return converted_list
|
||||||
|
|
||||||
|
|
||||||
def toGrid(input,parser=None):
|
def toGrid(input,parser=None):
|
||||||
grid = []
|
grid = []
|
||||||
if parser:
|
if parser:
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
import sys,re
|
import sys,re
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
sys.path.insert(0, '../../')
|
sys.path.insert(0, '../../')
|
||||||
from fred import list2int,get_re,nprint,lprint
|
from fred import list2int,get_re,nprint,lprint,loadFile
|
||||||
|
|
||||||
input_f = 'test'
|
input_f = 'test'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user