From c111075e16c939490853d913a4c0d5229ce40330 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sun, 10 Dec 2023 06:30:14 +0000 Subject: [PATCH] day 10 part 2 --- day10/sample10.in | 9 +++++++ day10/sample15.in | 10 +++++++ day10/sol.py | 66 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 day10/sample10.in create mode 100644 day10/sample15.in diff --git a/day10/sample10.in b/day10/sample10.in new file mode 100644 index 0000000..bd9cdf5 --- /dev/null +++ b/day10/sample10.in @@ -0,0 +1,9 @@ +........... +.S-------7. +.|F-----7|. +.||.....||. +.||.....||. +.|L-7.F-J|. +.|..|.|..|. +.L--J.L--J. +........... diff --git a/day10/sample15.in b/day10/sample15.in new file mode 100644 index 0000000..8f950ae --- /dev/null +++ b/day10/sample15.in @@ -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 diff --git a/day10/sol.py b/day10/sol.py index 35f9e43..22272d4 100644 --- a/day10/sol.py +++ b/day10/sol.py @@ -62,11 +62,73 @@ def find_loop(grid, start): return {} 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]: grid[start] = D loop = find_loop(grid, start) if loop: print(D, loop) print(max(loop.values())) - + print(enclosedby(grid, loop))