From 88b01fc9bdcf9c4d7ceaf96951bd6fc907dfdad7 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Thu, 5 Dec 2024 19:47:18 +0100 Subject: [PATCH] Working on 2015/07 --- 2015/07/solution.py | 68 +++++++++++++++++++++++++++++++ __pycache__/fred.cpython-311.pyc | Bin 9260 -> 10396 bytes fred.py | 26 ++++++++++++ solution.py | 2 +- 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 2015/07/solution.py 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 1512d8f59b79c8c726d7c9c58eb1de473d6f3cde..0890a2d96de3b92567ad6d71bc2fa18fb54e907c 100644 GIT binary patch delta 2541 zcmZ`)?QaxC7~k35yX)QUy|vf(Ub}6v?MgX71bk`0@~TKKK@g$Dl%q2(C(G?QyQQGT zHipEiM3hMY4JsrUV=yMt#27xoUmzHw%^DICKM@l>gCB?=)aRMImY3jW?>95gJTuR4 zp654vUq63#Tl~vdETX`3^S5nd*XlotH%)y5qtgmiD6y5HN=t?aRgWn{D%Air+D7$b zWJsq07N9}+g;;PjNNbMiLm?VwH8jF%Ms*s6ahMt~r_vboBQO`Gac00=0>&|#gx%sa z1(={2TFa8Oj_rxUboy9eC`IdGqLwxQ*3m}5G;IQ`r|ql)lq}s36^f;CZ+O%X*o7iJ}ca$@lEgkV|Y$s1QfisU)DY~CRXIhyv;?o;%hGR2h?`Do?%8DB3 zJF{pm-BeacxKF~&hB+g3V(WWb&!j(Z{iOBd%-PI!qxXi`i0*WV>3>w;1Lmy0ruGSI2M<@IOxK z1yYTf7NJ;NTuvp#fSPLR=~g^URrOh<@Fue0_sj@y!#;VQsfEK_;=(@et#lnK$i%>zU4|iG?YSxu)lsg`&sC7({yew*-}r0j4tr@RgK%pkM_JeWY;oyeDX z7uYSF+biOsmExt)#hyjz{}|d-fO!_-Jf%CvOEz;k`VWZinryTiU7G+#h`3do7NwdC zy}dBBh#wen#)}@*Rn0Erb@P5z3e(&W1K}(g6bHgjWpNi+p}EYnh}_P_FWlZrp~Itv zBHb`vg22nbx>j5d_mgh%Px$2}exL+*!Lw8@IWLFl2RH|9InSSjiS^>G2&|jE7+FPP zCKzNTpeilm_vordsi`PCT*?>e@EE3h0DLx!XN~g^?51(?snxg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As`)`?fDr$%K{3Rq_7Nt-QM&JwjayXaf4!&kxhLg$C- zHRTF6m}`hZsn<=lF2UE$JU^sLEnGy>hL7{|#xc}~Zc+I74o6U0X-WV9 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'