From 59667eac067266104b62f3f7bfc682b1aa40eada Mon Sep 17 00:00:00 2001 From: setop Date: Sun, 3 Dec 2023 00:24:52 +0100 Subject: [PATCH] day 7, fun after all ; should make a movie --- d07/graph.awk | 33 +++++++++++++++++++++++++++++++++ d07/graph2.awk | 40 ++++++++++++++++++++++++++++++++++++++++ d07/run.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 d07/graph.awk create mode 100644 d07/graph2.awk create mode 100644 d07/run.py diff --git a/d07/graph.awk b/d07/graph.awk new file mode 100644 index 0000000..b87883f --- /dev/null +++ b/d07/graph.awk @@ -0,0 +1,33 @@ +BEGIN { + print "digraph {" +} + +function t(s) { + if (s ~ /^[[:digit:]]+$/) { + print "S" NR "[label=\"" s "\"];" + return "S" NR + } else { + return s + } +} + +NF == 3 { # register or int + print t($1), "->", $3 +} + +NF == 4 { # unary op + print "O" NR, "[shape=box,label=\"" $1 "\"];" + print t($2), "->", "O" NR ";" + print "O" NR, "->", $4 ";" +} + +NF == 5 { # binary op + print "O" NR, "[shape=box,label=\"" $2 "\"]" + print t($1), "->", "O" NR ";" + print t($3), "->", "O" NR ";" + print "O" NR, "->", $5 ";" +} + +END { + print "}" +} \ No newline at end of file diff --git a/d07/graph2.awk b/d07/graph2.awk new file mode 100644 index 0000000..b569019 --- /dev/null +++ b/d07/graph2.awk @@ -0,0 +1,40 @@ +BEGIN { + print "digraph {" +} + +function t(s) { + if (s ~ /^[[:digit:]]+$/) { + print "S" NR, "[shape=doublecircle,label=\"" s "\"];" + return "S" NR + } else { + return s + } +} + +NF == 3 { # register or int + print t($1), "->", $3 +} + +NF == 4 { # unary op + print "O" NR, "[shape=box,label=\"" $1 "\"];" + print t($2), "->", "O" NR ";" + print "O" NR, "->", $4 ";" +} + +NF == 5 && substr($2,2) == "SHIFT" { # binary op (R/L)SHIFT + print "O" NR, "[shape=box,label=\"" $2 " " $3 "\"]" + print t($1), "->", "O" NR ";" + print "O" NR, "->", $5 ";" + next +} + +NF == 5 { # binary op, AND or OR + print "O" NR, "[shape=box,label=\"" $2 "\"]" + print t($1), "->", "O" NR ";" + print t($3), "->", "O" NR ";" + print "O" NR, "->", $5 ";" +} + +END { + print "}" +} \ No newline at end of file diff --git a/d07/run.py b/d07/run.py new file mode 100644 index 0000000..0d8851e --- /dev/null +++ b/d07/run.py @@ -0,0 +1,39 @@ +import sys + +R = dict() # registers + +for l in sys.stdin.readlines(): + v, k = l[:-1].split(' -> ') + R[k] = v + +A = 2**16 -1 # FFFF + +def val(k:str) -> int: + if k.isdigit(): + return int(k) + v = R[k] + if type(v) == int: # k is resolved + return v + r = None + I = v.split(' ') + if len(I) == 1: + r = val(I[0]) + else: + i = I[-2] + if i == "AND": + r = val(I[0]) & val(I[2]) + elif i == "OR": + r = val(I[0]) | val(I[2]) + elif i == "LSHIFT": + r = val(I[0]) << val(I[2]) + elif i == "RSHIFT": + r = val(I[0]) >> val(I[2]) + elif i == "RSHIFT": + r = val(I[0]) >> val(I[2]) + elif i == "NOT": + r = ~ val(I[1]) + r &= A + R[k] = r + return r + +print(val(sys.argv[1])) \ No newline at end of file