From 1b5ec9d139fd737db6b83f4dd04f5837dac09b43 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 10 Dec 2024 09:30:35 +0100 Subject: [PATCH] day10 - cleaned up --- day10.py | 52 +++++++++----------------------------------- inputs/input10_test2 | 4 ---- inputs/input10_test3 | 7 ------ inputs/input10_test4 | 7 ------ inputs/input10_test5 | 7 ------ inputs/input10_test7 | 7 ------ 6 files changed, 10 insertions(+), 74 deletions(-) delete mode 100644 inputs/input10_test2 delete mode 100644 inputs/input10_test3 delete mode 100644 inputs/input10_test4 delete mode 100644 inputs/input10_test5 delete mode 100644 inputs/input10_test7 diff --git a/day10.py b/day10.py index 38f400f..c6e72b7 100644 --- a/day10.py +++ b/day10.py @@ -6,71 +6,39 @@ from tools.grid import Grid from typing import Any -def find_nine(grid: Grid, start: Coordinate) -> int: - q = deque([start]) - seen = set() - nines = set() - while q: - pos = q.popleft() - cur_val = grid.get(pos) - if cur_val == 9: - nines.add(pos) - continue - if pos in seen: - continue - seen.add(pos) - - for next_pos in grid.getNeighboursOf(pos, includeDiagonal=False): - if grid.get(next_pos) == cur_val + 1: - q.append(next_pos) - - return len(nines) - - -def find_nine_ways(grid: Grid, start: Coordinate, next_val: int = 1) -> int: +def find_nine_ways(grid: Grid, start: Coordinate, next_val: int = 1) -> list[Coordinate]: if next_val == 9: - return sum(1 for x in grid.getNeighboursOf(start, includeDiagonal=False) if grid.get(x) == 9) + return [x for x in grid.getNeighboursOf(start, includeDiagonal=False) if grid.get(x) == 9] else: - return sum( - find_nine_ways(grid, x, next_val + 1) - for x in grid.getNeighboursOf(start, includeDiagonal=False) - if grid.get(x) == next_val - ) + ret = [] + for x in grid.getNeighboursOf(start, includeDiagonal=False): + if grid.get(x) == next_val: + ret += find_nine_ways(grid, x, next_val + 1) + return ret class Day(AOCDay): inputs = [ [ - (1, "input10_test2"), - (2, "input10_test3"), - (4, "input10_test4"), - (3, "input10_test5"), (36, "input10_test"), (644, "input10"), ], [ - (3, "input10_test7"), (81, "input10_test"), (1366, "input10"), ], ] def parse_input(self) -> Grid: - return Grid.from_data(self.getInput(), default=0, translate={"[0-9]": int}) + return Grid.from_data(self.getInput(), default=-1, translate={"[0-9]": int}) def part1(self) -> Any: grid = self.parse_input() - trail_heads = [ - Coordinate(x, y) for y in grid.rangeY() for x in grid.rangeX() if grid.get(Coordinate(x, y)) == 0 - ] - return sum(find_nine(grid, trail) for trail in trail_heads) + return sum(len(set(find_nine_ways(grid, trail))) for trail in grid.find(0)) def part2(self) -> Any: grid = self.parse_input() - trail_heads = [ - Coordinate(x, y) for y in grid.rangeY() for x in grid.rangeX() if grid.get(Coordinate(x, y)) == 0 - ] - return sum(find_nine_ways(grid, trail) for trail in trail_heads) + return sum(len(find_nine_ways(grid, trail)) for trail in grid.find(0)) if __name__ == "__main__": diff --git a/inputs/input10_test2 b/inputs/input10_test2 deleted file mode 100644 index dd80454..0000000 --- a/inputs/input10_test2 +++ /dev/null @@ -1,4 +0,0 @@ -0123 -1234 -8765 -9876 \ No newline at end of file diff --git a/inputs/input10_test3 b/inputs/input10_test3 deleted file mode 100644 index 376ee8e..0000000 --- a/inputs/input10_test3 +++ /dev/null @@ -1,7 +0,0 @@ -...0... -...1... -...2... -6543456 -7.....7 -8.....8 -9.....9 \ No newline at end of file diff --git a/inputs/input10_test4 b/inputs/input10_test4 deleted file mode 100644 index 16c5076..0000000 --- a/inputs/input10_test4 +++ /dev/null @@ -1,7 +0,0 @@ -..90..9 -...1.98 -...2..7 -6543456 -765.987 -876.... -987.... \ No newline at end of file diff --git a/inputs/input10_test5 b/inputs/input10_test5 deleted file mode 100644 index 2e42d65..0000000 --- a/inputs/input10_test5 +++ /dev/null @@ -1,7 +0,0 @@ -10..9.. -2...8.. -3...7.. -4567654 -...8..3 -...9..2 -.....01 \ No newline at end of file diff --git a/inputs/input10_test7 b/inputs/input10_test7 deleted file mode 100644 index 98ab931..0000000 --- a/inputs/input10_test7 +++ /dev/null @@ -1,7 +0,0 @@ -.....0. -..4321. -..5..2. -..6543. -..7..4. -..8765. -..9.... \ No newline at end of file