day 10 part 2
parent
a395a39fac
commit
c111075e16
|
@ -0,0 +1,9 @@
|
||||||
|
...........
|
||||||
|
.S-------7.
|
||||||
|
.|F-----7|.
|
||||||
|
.||.....||.
|
||||||
|
.||.....||.
|
||||||
|
.|L-7.F-J|.
|
||||||
|
.|..|.|..|.
|
||||||
|
.L--J.L--J.
|
||||||
|
...........
|
|
@ -0,0 +1,10 @@
|
||||||
|
FF7FSF7F7F7F7F7F---7
|
||||||
|
L|LJ||||||||||||F--J
|
||||||
|
FL-7LJLJ||||||LJL-77
|
||||||
|
F--JF--7||LJLJ7F7FJ-
|
||||||
|
L---JF-JLJ.||-FJLJJ7
|
||||||
|
|F|F-JF---7F7-L7L|7|
|
||||||
|
|FFJF7L7F-JF7|JL---7
|
||||||
|
7-L-JL7||F7|L7F-7F7|
|
||||||
|
L.L7LFJ|||||FJL7||LJ
|
||||||
|
L7JLJL-JLJLJL--JLJ.L
|
64
day10/sol.py
64
day10/sol.py
|
@ -63,10 +63,72 @@ def find_loop(grid, start):
|
||||||
|
|
||||||
return dist
|
return dist
|
||||||
|
|
||||||
|
def enclosedby(grid, points):
|
||||||
|
# upsample by 3x3
|
||||||
|
fill = {}
|
||||||
|
for i,j in grid:
|
||||||
|
for k in range(3):
|
||||||
|
for l in range(3):
|
||||||
|
fill[i*3+k,j*3+l] = 0
|
||||||
|
print(*sorted(fill.keys()), sep="\n")
|
||||||
|
for i,j in points:
|
||||||
|
d = grid[i,j]
|
||||||
|
if d == N|S: t=[[0,1,0],[0,1,0],[0,1,0]]
|
||||||
|
elif d == N|W: t=[[0,1,0],[1,1,0],[0,0,0]]
|
||||||
|
elif d == N|E: t=[[0,1,0],[0,1,1],[0,0,0]]
|
||||||
|
elif d == S|W: t=[[0,0,0],[1,1,0],[0,1,0]]
|
||||||
|
elif d == S|E:t=[[0,0,0],[0,1,1],[0,1,0]]
|
||||||
|
elif d == E|W:t=[[0,0,0],[1,1,1],[0,0,0]]
|
||||||
|
else: raise Exception("invalid d=%d at (%s,%s)" % (d, i, j))
|
||||||
|
for k in range(3):
|
||||||
|
for l in range(3):
|
||||||
|
fill[i*3+k,j*3+l] = t[k][l]
|
||||||
|
print(fill)
|
||||||
|
|
||||||
|
# fill edges
|
||||||
|
for i in range(Y*3):
|
||||||
|
if fill[i,0] == 0: fill[i,0] = 2
|
||||||
|
if fill[i,X*3-1] == 0: fill[i,X*3-1] = 2
|
||||||
|
for j in range(X*3):
|
||||||
|
if fill[0,j] == 0: fill[0,j] = 2
|
||||||
|
if fill[Y*3-1,j] == 0: fill[Y*3-1,j] = 2
|
||||||
|
|
||||||
|
# flood fill
|
||||||
|
while True:
|
||||||
|
#for i in range(Y*3):
|
||||||
|
#print("".join(".+O"[fill[i,j]] for j in range(X*3)))
|
||||||
|
next = fill.copy()
|
||||||
|
def set(i,j):
|
||||||
|
if (i,j) in fill:
|
||||||
|
if fill[i,j] == 0:
|
||||||
|
next[i,j] = 2
|
||||||
|
for i,j in fill:
|
||||||
|
if fill[i,j] == 2:
|
||||||
|
set(i-1,j)
|
||||||
|
set(i+1,j)
|
||||||
|
set(i,j-1)
|
||||||
|
set(i,j+1)
|
||||||
|
if next == fill:
|
||||||
|
break
|
||||||
|
fill = next
|
||||||
|
for i in range(Y*3):
|
||||||
|
print("".join(".+O"[fill[i,j]] for j in range(X*3)))
|
||||||
|
|
||||||
|
# downsample
|
||||||
|
inside = []
|
||||||
|
for i,j in grid:
|
||||||
|
f = [fill[i*3+k,j*3+l]==0 for k in range(3) for l in range(3)]
|
||||||
|
if all(f):
|
||||||
|
inside.append((i,j))
|
||||||
|
|
||||||
|
|
||||||
|
print(inside)
|
||||||
|
return len(inside)
|
||||||
|
|
||||||
for D in [W|S, N|W, N|E, S|E, N|S, W|E]:
|
for D in [W|S, N|W, N|E, S|E, N|S, W|E]:
|
||||||
grid[start] = D
|
grid[start] = D
|
||||||
loop = find_loop(grid, start)
|
loop = find_loop(grid, start)
|
||||||
if loop:
|
if loop:
|
||||||
print(D, loop)
|
print(D, loop)
|
||||||
print(max(loop.values()))
|
print(max(loop.values()))
|
||||||
|
print(enclosedby(grid, loop))
|
||||||
|
|
Loading…
Reference in New Issue