Compare commits
9 Commits
e127af3e0d
...
main
Author | SHA1 | Date | |
---|---|---|---|
a2f903affc | |||
c1e2a28ac5 | |||
5c6f10c81e | |||
6eff8af332 | |||
067decada0 | |||
df55eec6d8 | |||
199bc88505 | |||
59667eac06 | |||
69f4c3c8e7 |
33
d07/graph.awk
Normal file
33
d07/graph.awk
Normal 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
40
d07/graph2.awk
Normal 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
39
d07/run.py
Normal 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
11
d08/run.py
Normal 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)
|
22
d09/run.py
Normal file
22
d09/run.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import sys
|
||||
from itertools import permutations as perm
|
||||
|
||||
C = set() # cities
|
||||
D = dict() # distances
|
||||
|
||||
for line in sys.stdin.readlines():
|
||||
c1, _, c2, _, d = line[:-1].split()
|
||||
d = int(d)
|
||||
D[(c1, c2)] = d
|
||||
D[(c2, c1)] = d
|
||||
C.add(c1)
|
||||
C.add(c2)
|
||||
|
||||
S = 999999
|
||||
L = 0
|
||||
for r in perm(C): # brut force all possible routes
|
||||
l = sum(D[(c1, c2)]
|
||||
for c1, c2 in zip(r, r[1:]))
|
||||
S = min(l, S)
|
||||
L = max(l, L)
|
||||
print(S, L)
|
7
d10/run.py
Normal file
7
d10/run.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import sys
|
||||
from itertools import groupby as gb
|
||||
|
||||
r = sys.argv[1]
|
||||
for _ in range(int(sys.argv[2])):
|
||||
r = "".join(f'{n}{c}' for c,n in map(lambda x: (x[0],len(list(x[1]))), gb(r)))
|
||||
print(len(r))
|
40
d11/run.py
Normal file
40
d11/run.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import sys
|
||||
|
||||
letters = 'abcdefghijklmnopqrstuvwxyz'
|
||||
doubles = [c+c for c in letters]
|
||||
straights = [a+b+c for a,b,c in zip(letters, letters[1:], letters[2:])]
|
||||
next_letter = {c1: c2 for c1, c2 in zip(letters, letters[1:]+'a')}
|
||||
|
||||
|
||||
def is_valid(s):
|
||||
# cannot contain i, o, or l
|
||||
if 'i' in s or 'o' in s or 'l' in s:
|
||||
return False
|
||||
# must include two different pairs of letters
|
||||
if sum([d in s for d in doubles]) < 2:
|
||||
return False
|
||||
# must include a straight of length 3 or greater
|
||||
if not any([d in s for d in straights]):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def next_password(s):
|
||||
s = s[:-1] + next_letter[s[-1]] # increment the last letter
|
||||
for i in range(-1, -8, -1):
|
||||
if s[i] == 'a': # increment n-1 letter is n letter changed to 'a'
|
||||
s = s[:i-1] + next_letter[s[i-1]] + s[i:]
|
||||
else:
|
||||
break
|
||||
return s
|
||||
|
||||
|
||||
password = sys.argv[1]
|
||||
while not is_valid(password):
|
||||
password = next_password(password)
|
||||
print(password)
|
||||
|
||||
password = next_password(password)
|
||||
while not is_valid(password):
|
||||
password = next_password(password)
|
||||
print(password)
|
3
d12/cmd.txt
Normal file
3
d12/cmd.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
jq '.' input | grep -o -E '\-?[0-9]+' | sum0
|
||||
|
23
d12/run.py
Normal file
23
d12/run.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import sys
|
||||
import json
|
||||
|
||||
|
||||
def sum_of_item(item, skip_red=False):
|
||||
|
||||
if isinstance(item, list):
|
||||
return sum(sum_of_item(i, skip_red) for i in item)
|
||||
|
||||
if isinstance(item, dict):
|
||||
if skip_red and 'red' in item.values():
|
||||
return 0
|
||||
return sum(sum_of_item(i, skip_red) for i in item.values())
|
||||
|
||||
if isinstance(item, str):
|
||||
return 0
|
||||
|
||||
if isinstance(item, int):
|
||||
return item
|
||||
|
||||
abacus = json.load(sys.stdin)
|
||||
print(sum_of_item(abacus))
|
||||
print(sum_of_item(abacus, skip_red=True))
|
26
d13/run.py
Normal file
26
d13/run.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import sys, os
|
||||
from collections import defaultdict as dd
|
||||
from itertools import permutations as PERM
|
||||
|
||||
N:set[str] = set()
|
||||
D:dict[tuple[str,str],int] = dd(int)
|
||||
|
||||
for l in sys.stdin.readlines():
|
||||
[a,s,b] = l[:-1].split(" ")
|
||||
D[(a,b)] += int(s)
|
||||
D[(b,a)] += int(s)
|
||||
N.add(a)
|
||||
N.add(b)
|
||||
|
||||
if len(sys.argv)>1 and sys.argv[1]=="2":
|
||||
# add you
|
||||
for a in N:
|
||||
D[(a,'Y')] = 0
|
||||
D[('Y',a)] = 0
|
||||
N.add('Y')
|
||||
|
||||
def score(chain:list[str]) -> int:
|
||||
return sum(D[(a,b)] for (a,b) in zip(chain, chain[1:])) \
|
||||
+ D[(chain[0],chain[-1])]
|
||||
|
||||
print(max(score(p) for p in PERM(N)))
|
41
d14/run.py
Normal file
41
d14/run.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from math import log10
|
||||
import sys, os
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Rein:
|
||||
name: str
|
||||
speed: int
|
||||
duration: int
|
||||
rest: int
|
||||
distance: int = 0
|
||||
score: int = 0
|
||||
|
||||
D:list[Rein] = []
|
||||
|
||||
for l in sys.stdin.readlines():
|
||||
L = l[:-1].split(" ")
|
||||
D.append(Rein(L[0],int(L[3]),int(L[6]),int(L[13])))
|
||||
|
||||
howlong = int(sys.argv[1])
|
||||
|
||||
lead_distance = 0
|
||||
for t in range(howlong):
|
||||
# increment each rein
|
||||
for rein in D:
|
||||
period = rein.duration + rein.rest
|
||||
(p, m) = divmod(t, period)
|
||||
# is flying or at rest ?
|
||||
if m < rein.duration:
|
||||
rein.distance += rein.speed
|
||||
lead_distance = max(lead_distance, rein.distance)
|
||||
# compute score
|
||||
for rein in D:
|
||||
if rein.distance == lead_distance:
|
||||
rein.score += 1
|
||||
# part1
|
||||
print(lead_distance)
|
||||
# part2
|
||||
print(max(rein.score for rein in D))
|
||||
|
40
d15/run.py
Normal file
40
d15/run.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from dataclasses import dataclass
|
||||
import sys
|
||||
|
||||
@dataclass
|
||||
class Ing:
|
||||
capacity: int
|
||||
durability: int
|
||||
flavor: int
|
||||
texture: int
|
||||
calorie: int
|
||||
|
||||
def __add__(self, j):
|
||||
return Ing(self.capacity+j.capacity,self.durability+j.durability, self.flavor+j.flavor, self.texture+j.texture,self.calorie+j.calorie)
|
||||
|
||||
def pIng(self, p:int):
|
||||
return Ing(self.capacity*p, self.durability*p, self.flavor*p, self.texture*p, self.calorie*p)
|
||||
|
||||
def score(self):
|
||||
if self.capacity<0 or self.durability<0 or self.flavor<0 or self.texture<0:
|
||||
return 0
|
||||
return self.capacity * self.durability * self.flavor * self.texture
|
||||
|
||||
L:list[Ing] = list()
|
||||
|
||||
for l in sys.stdin.readlines():
|
||||
l = l.replace(",","").split(" ")
|
||||
L.append(Ing(int(l[2]),int(l[4]),int(l[6]),int(l[8]),int(l[10])))
|
||||
|
||||
M1 = M2 = 0
|
||||
for p1 in range(1,98):
|
||||
for p2 in range(1,98):
|
||||
for p3 in range(1,98):
|
||||
p4 = 100-(p1+p2+p3)
|
||||
i:Ing = L[0].pIng(p1)+L[1].pIng(p2)+L[2].pIng(p3)+L[3].pIng(p4)
|
||||
s = i.score()
|
||||
M1 = max(M1, s)
|
||||
if i.calorie == 500:
|
||||
M2 = max(M2, s)
|
||||
|
||||
print(M1, M2)
|
Reference in New Issue
Block a user