day 7, fun after all ; should make a movie

This commit is contained in:
setop 2023-12-03 00:24:52 +01:00
parent 69f4c3c8e7
commit 59667eac06
3 changed files with 112 additions and 0 deletions

33
d07/graph.awk Normal file
View File

@ -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 "}"
}

40
d07/graph2.awk Normal file
View File

@ -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 "}"
}

39
d07/run.py Normal file
View File

@ -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]))