diff --git a/d21/draw.awk b/d21/draw.awk new file mode 100644 index 0000000..82660f2 --- /dev/null +++ b/d21/draw.awk @@ -0,0 +1,9 @@ +BEGIN { print "digraph G {"} +{ k = substr($1,1,length($1)-1) } +NF == 2 { print k, "[label=\"" $0 "\"];" } +NF == 4 { +print k, "[label=\"" k " " $3 "\"];" +print k, "->", $2, ";" +print k, "->", $4, ";" +} +END { print "}" } diff --git a/d21/part1.py b/d21/part1.py new file mode 100644 index 0000000..3fa097d --- /dev/null +++ b/d21/part1.py @@ -0,0 +1,28 @@ +import sys + +N = dict() # numbers +E = dict() # expressions +for l in sys.stdin.read().splitlines(): + k, v = l.split(":") + v = v.strip() + if v.isdigit(): + N[k]=int(v) + else: + w = v.split(" ") + E[k]=w +O = { + '*': lambda x,y: x*y, + '+': lambda x,y: x+y, + '-': lambda x,y: x-y, + '/': lambda x,y: x/y, +} +while len(E)>0: + D = dict() + for k,v in E.items(): + (a,o,b) = v + if a in N and b in N: + N[k] = O[o](N[a],N[b]) + else: + D[k] = v + E = D +print(N["root"]) \ No newline at end of file diff --git a/d21/part2.py b/d21/part2.py new file mode 100644 index 0000000..e7e9e91 --- /dev/null +++ b/d21/part2.py @@ -0,0 +1,43 @@ +import sys + +N = dict() # numbers +E = list() # expressions +I = {"+":"-","-":"+","*":"/","/":"*"} +for l in sys.stdin.read().splitlines(): + k, v = l.split(":") + v = v.strip() + if v.isdigit(): + N[k]=int(v) + else: + (a,o,b) = v.split(" ") + E.append((k,a,o,b)) + if o in {"+","*"}: + E.append((a,k,I[o],b)) + E.append((b,k,I[o],a)) + else: + E.append((a,k,I[o],b)) + E.append((b,a,o,k)) +if len(N)<100: + N["pppw"] = 150 +else: + N["qmfl"] = 54426117311903 + N["qdpj"] = 54426117311903 +del N["humn"] +O = { + '/': lambda x,y: x/y, + '-': lambda x,y: x-y, + '+': lambda x,y: x+y, + '*': lambda x,y: x*y, +} +while len(E)>0: + D = list() + for (k,a,o,b) in E: + if a in N and b in N: + N[k] = O[o](N[a],N[b]) + if k == "humn": + print(N[k]) + D = {} + break + else: + D.append((k,a,o,b)) + E = D