Compare commits

...

2 Commits

5 changed files with 161 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]))

11
d08/run.py Normal file
View File

@ -0,0 +1,11 @@
import sys
R = T = N = 0
for l in sys.stdin.readlines():
l = l[:-1]
s = eval(l)
T += len(l)
R += len(s)
N += sum((2 if c in ['"', '\\'] else 1 for c in l), 2)
#print(len(l),l,len(s), s, nl)
print(T, R, T-R, N-T)

38
d09/part1.py Normal file
View File

@ -0,0 +1,38 @@
import sys
import networkx as nx
G = nx.Graph()
D = {}
for l in sys.stdin.readlines():
l = l[:-1]
I = l.split(' ')
G.add_edge(I[0], I[2], weight=int(I[4]))
#G.add_edge(I[2], I[0], weight=int(I[4]))
D[(I[0], I[2])]=int(I[4])
D[(I[2], I[0])]=int(I[4])
def dfs_visit(graph, node, visited):
visited[node] = True
for neighbor in graph.neighbors(node):
if not visited[neighbor]:
dfs_visit(graph, neighbor, visited)
def dfs(graph):
visited = node: False for node in graph.nodes
for node in graph.nodes:
if not visited[node]:
dfs_visit(graph, node, visited)
# Call the DFS function and visit all nodes exactly once
dfs(G)
tsp = nx.approximation.traveling_salesman_problem
L = tsp(G)#, cycle=False)
print(L)
S = sum(D[(a,b)] for (a,b) in zip(L[1:],L[2:]))
print(S)