diff --git a/2015/07/solution.py b/2015/07/solution.py new file mode 100644 index 0000000..5d2964d --- /dev/null +++ b/2015/07/solution.py @@ -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() diff --git a/__pycache__/fred.cpython-311.pyc b/__pycache__/fred.cpython-311.pyc index 1512d8f..0890a2d 100644 Binary files a/__pycache__/fred.cpython-311.pyc and b/__pycache__/fred.cpython-311.pyc differ diff --git a/fred.py b/fred.py index 79a34b5..7d71c87 100644 --- a/fred.py +++ b/fred.py @@ -1,5 +1,31 @@ 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): grid = [] if parser: diff --git a/solution.py b/solution.py index d3d1904..4363a57 100644 --- a/solution.py +++ b/solution.py @@ -2,7 +2,7 @@ import sys,re from pprint import pprint sys.path.insert(0, '../../') -from fred import list2int,get_re,nprint,lprint +from fred import list2int,get_re,nprint,lprint,loadFile input_f = 'test'