Compare commits

...

2 Commits

Author SHA1 Message Date
setop 3142bc1ebb day 10, part 1 ok, part 2 with help, but nice drawing 2023-12-10 15:56:37 +01:00
setop 95665139a7 day 9, refactor 2023-12-09 13:50:09 +01:00
4 changed files with 71 additions and 22 deletions

View File

@ -1,11 +0,0 @@
import sys
L = [list(map(int,l.split(' '))) for l in sys.stdin.read().splitlines()]
def extrapolate(L):
if all(not a for a in L):
return L+[0]
M = [b-a for a,b in zip(L,L[1:])]
return L+[L[-1]+extrapolate(M)[-1]]
print(sum(extrapolate(l)[-1] for l in L))

View File

@ -1,11 +0,0 @@
import sys
L = [list(map(int,l.split(' '))) for l in sys.stdin.read().splitlines()]
def extrapolate(L):
if all(not a for a in L):
return [0]+L
M = [b-a for a,b in zip(L,L[1:])]
return [L[0]-extrapolate(M)[0]]+L
print(sum(extrapolate(l)[0] for l in L))

12
d09/run.py Normal file
View File

@ -0,0 +1,12 @@
import sys
L = [list(map(int,l.split(' '))) for l in sys.stdin.read().splitlines()]
def extrapolate(L):
if all(a == 0 for a in L):
return 0
M = [b-a for a,b in zip(L,L[1:])]
return L[-1]+extrapolate(M)
print(sum(extrapolate(l) for l in L))
print(sum(extrapolate(l[::-1]) for l in L))

59
d10/run.py Normal file
View File

@ -0,0 +1,59 @@
import sys
# parse
I = sys.stdin.read().splitlines()
G = [list(l) for l in I]
W = len(G[0])
H = len(G)
# look for S
S = None
for y in range(H):
if (x := I[y].find("S")) > -1:
S = (x,y)
break
# encode moves
D = {
'u': {'|': ( 0,-1,'u'), 'F': ( 1, 0,'r'), '7': (-1, 0,'l')},
'r': {'-': ( 1, 0,'r'), 'J': ( 0,-1,'u'), '7': ( 0, 1,'d')},
'd': {'|': ( 0, 1,'d'), 'L': ( 1, 0,'r'), 'J': (-1, 0,'l')},
'l': {'-': (-1, 0,'l'), 'L': ( 0,-1,'u'), 'F': ( 0, 1,'d')},
}
# part 1
poly = [S] # vertices of the polygone
x0, y0 = S
P = (x0+1, y0, 'r') # start at S and go right
for _ in range(100_000):
x, y, d = P
poly.append((x,y))
z = G[y][x]
if z == 'S':
break
dx,dy,nd = D[d].get(z)
P = (x+dx,y+dy,nd)
print(len(poly)//2)
# part 2
from matplotlib.path import Path
pg = Path(poly)
J = [['.']*W for _ in range(H)]
R = 0
for y in range(W):
for x in range(H):
if (x,y) in poly:
J[y][x] = G[y][x]
elif pg.contains_point((x,y)):
R += 1
J[y][x] = "O"
print(R)
# plot
J[y0][x0] = "S"
T = str.maketrans("|-LJ7F", "│─└┘┐┌")
with open("grid.txt","wt") as f:
for row in J:
print("".join(row).translate(T), file=f)