generated from public/aoc_template
Day 10 - cleanup
This commit is contained in:
parent
c83cb8598b
commit
606a4bf4b2
134
day10.py
134
day10.py
@ -4,85 +4,8 @@ from tools.coordinate import Coordinate
|
|||||||
from tools.grid import Grid
|
from tools.grid import Grid
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
VALID_NEIGHBOURS = {
|
|
||||||
Coordinate(1, 0): {
|
|
||||||
"check": ["F", "-", "L", "S"],
|
|
||||||
"valid": ["7", "-", "J"],
|
|
||||||
},
|
|
||||||
Coordinate(0, 1): {
|
|
||||||
"check": ["7", "|", "F", "S"],
|
|
||||||
"valid": ["J", "|", "L"],
|
|
||||||
},
|
|
||||||
Coordinate(-1, 0): {
|
|
||||||
"check": ["J", "-", "7", "S"],
|
|
||||||
"valid": ["F", "-", "L"],
|
|
||||||
},
|
|
||||||
Coordinate(0, -1): {
|
|
||||||
"check": ["L", "|", "J", "S"],
|
|
||||||
"valid": ["F", "|", "7"],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
LOOKUP = {
|
||||||
def remove_clutter(grid: Grid, start: Coordinate):
|
|
||||||
pipe_coords = set()
|
|
||||||
pipe_coords.add(start)
|
|
||||||
q = deque()
|
|
||||||
q.append(start)
|
|
||||||
while q:
|
|
||||||
cur = q.pop()
|
|
||||||
for c, v in VALID_NEIGHBOURS.items():
|
|
||||||
dc = cur + c
|
|
||||||
if dc not in pipe_coords and grid.get(cur) in v["check"] and grid.get(dc) in v["valid"]:
|
|
||||||
pipe_coords.add(dc)
|
|
||||||
q.append(dc)
|
|
||||||
|
|
||||||
for c in grid.getActiveCells():
|
|
||||||
if c not in pipe_coords:
|
|
||||||
grid.set(c, False)
|
|
||||||
|
|
||||||
|
|
||||||
def get_farthest(grid: Grid, start: Coordinate) -> int:
|
|
||||||
q = deque()
|
|
||||||
q.append((0, start))
|
|
||||||
visited = set()
|
|
||||||
max_dist = 0
|
|
||||||
while q:
|
|
||||||
dist, cur = q.popleft()
|
|
||||||
if cur in visited:
|
|
||||||
continue
|
|
||||||
visited.add(cur)
|
|
||||||
for c, v in VALID_NEIGHBOURS.items():
|
|
||||||
dc = cur + c
|
|
||||||
if dc not in visited and grid.get(cur) in v["check"] and grid.get(dc) in v["valid"]:
|
|
||||||
q.append((dist + 1, dc))
|
|
||||||
max_dist = max(max_dist, dist + 1)
|
|
||||||
|
|
||||||
return max_dist
|
|
||||||
|
|
||||||
|
|
||||||
def get_area(grid: Grid, start: Coordinate) -> set[Coordinate]:
|
|
||||||
if grid.get(start):
|
|
||||||
return set()
|
|
||||||
area = set()
|
|
||||||
q = deque()
|
|
||||||
q.append(start)
|
|
||||||
while q:
|
|
||||||
c = q.popleft()
|
|
||||||
if c in area:
|
|
||||||
continue
|
|
||||||
area.add(c)
|
|
||||||
for n in c.getNeighbours(includeDiagonal=True):
|
|
||||||
if grid.get(n):
|
|
||||||
continue
|
|
||||||
if grid.isWithinBoundaries(n):
|
|
||||||
q.append(n)
|
|
||||||
|
|
||||||
return area
|
|
||||||
|
|
||||||
|
|
||||||
def walk(grid: Grid) -> int:
|
|
||||||
LOOKUP = {
|
|
||||||
"|": {
|
"|": {
|
||||||
Coordinate(0, 1): {"o_dir": [Coordinate(-1, 0)], "i_dir": [Coordinate(1, 0)], "n_dir": Coordinate(0, 1)},
|
Coordinate(0, 1): {"o_dir": [Coordinate(-1, 0)], "i_dir": [Coordinate(1, 0)], "n_dir": Coordinate(0, 1)},
|
||||||
Coordinate(0, -1): {"o_dir": [Coordinate(1, 0)], "i_dir": [Coordinate(-1, 0)], "n_dir": Coordinate(0, -1)},
|
Coordinate(0, -1): {"o_dir": [Coordinate(1, 0)], "i_dir": [Coordinate(-1, 0)], "n_dir": Coordinate(0, -1)},
|
||||||
@ -139,8 +62,45 @@ def walk(grid: Grid) -> int:
|
|||||||
"n_dir": Coordinate(1, 0),
|
"n_dir": Coordinate(1, 0),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def remove_clutter(grid: Grid, start: Coordinate):
|
||||||
|
pipe_coords = set()
|
||||||
|
for x in LOOKUP[grid.get(start)]:
|
||||||
|
dir_ = LOOKUP[grid.get(start)][x]["n_dir"]
|
||||||
|
break
|
||||||
|
while start not in pipe_coords:
|
||||||
|
pipe_coords.add(start)
|
||||||
|
start += dir_
|
||||||
|
dir_ = LOOKUP[grid.get(start)][dir_]["n_dir"]
|
||||||
|
|
||||||
|
for c in grid.getActiveCells():
|
||||||
|
if c not in pipe_coords:
|
||||||
|
grid.set(c, False)
|
||||||
|
|
||||||
|
|
||||||
|
def get_area(grid: Grid, start: Coordinate) -> set[Coordinate]:
|
||||||
|
if grid.get(start):
|
||||||
|
return set()
|
||||||
|
area = set()
|
||||||
|
q = deque()
|
||||||
|
q.append(start)
|
||||||
|
while q:
|
||||||
|
c = q.popleft()
|
||||||
|
if c in area:
|
||||||
|
continue
|
||||||
|
area.add(c)
|
||||||
|
for n in c.getNeighbours(includeDiagonal=True):
|
||||||
|
if grid.get(n):
|
||||||
|
continue
|
||||||
|
if grid.isWithinBoundaries(n):
|
||||||
|
q.append(n)
|
||||||
|
|
||||||
|
return area
|
||||||
|
|
||||||
|
|
||||||
|
def walk(grid: Grid, part2: bool = False) -> int:
|
||||||
cur = None
|
cur = None
|
||||||
for x in grid.rangeX():
|
for x in grid.rangeX():
|
||||||
for y in grid.rangeY():
|
for y in grid.rangeY():
|
||||||
@ -152,14 +112,17 @@ def walk(grid: Grid) -> int:
|
|||||||
|
|
||||||
assert grid.get(cur) == "F"
|
assert grid.get(cur) == "F"
|
||||||
|
|
||||||
|
pipe_len = 0
|
||||||
direction = Coordinate(0, 1)
|
direction = Coordinate(0, 1)
|
||||||
outside = get_area(grid, cur + Coordinate(0, -1))
|
outside = get_area(grid, cur + Coordinate(0, -1))
|
||||||
inside = set()
|
inside = set()
|
||||||
visited = set()
|
visited = set()
|
||||||
while cur not in visited:
|
while cur not in visited:
|
||||||
|
pipe_len += 1
|
||||||
visited.add(cur)
|
visited.add(cur)
|
||||||
cur += direction
|
cur += direction
|
||||||
c = grid.get(cur)
|
c = grid.get(cur)
|
||||||
|
if part2:
|
||||||
oc = LOOKUP[c][direction]["o_dir"]
|
oc = LOOKUP[c][direction]["o_dir"]
|
||||||
ic = LOOKUP[c][direction]["i_dir"]
|
ic = LOOKUP[c][direction]["i_dir"]
|
||||||
|
|
||||||
@ -177,7 +140,10 @@ def walk(grid: Grid) -> int:
|
|||||||
|
|
||||||
direction = LOOKUP[c][direction]["n_dir"]
|
direction = LOOKUP[c][direction]["n_dir"]
|
||||||
|
|
||||||
|
if part2:
|
||||||
return len(inside)
|
return len(inside)
|
||||||
|
else:
|
||||||
|
return pipe_len // 2
|
||||||
|
|
||||||
|
|
||||||
class Day(AOCDay):
|
class Day(AOCDay):
|
||||||
@ -196,7 +162,7 @@ class Day(AOCDay):
|
|||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
def parse_input(self) -> (Grid, Coordinate):
|
def parse_input(self) -> Grid:
|
||||||
grid = Grid()
|
grid = Grid()
|
||||||
start = None
|
start = None
|
||||||
for y, line in enumerate(self.getInput()):
|
for y, line in enumerate(self.getInput()):
|
||||||
@ -227,15 +193,13 @@ class Day(AOCDay):
|
|||||||
grid.set(start, "-")
|
grid.set(start, "-")
|
||||||
|
|
||||||
remove_clutter(grid, start)
|
remove_clutter(grid, start)
|
||||||
return grid, start
|
return grid
|
||||||
|
|
||||||
def part1(self) -> Any:
|
def part1(self) -> Any:
|
||||||
grid, start = self.parse_input()
|
return walk(self.parse_input())
|
||||||
return get_farthest(grid, start)
|
|
||||||
|
|
||||||
def part2(self) -> Any:
|
def part2(self) -> Any:
|
||||||
grid, _ = self.parse_input()
|
return walk(self.parse_input(), True)
|
||||||
return walk(grid)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user