diff --git a/day15.py b/day15.py index 0a024bc..acd1ea2 100644 --- a/day15.py +++ b/day15.py @@ -4,26 +4,6 @@ from tools.grid import Grid from typing import Any, List -def getGrid(lines: List[str], multiply: bool = False) -> Grid: - size = len(lines) - g = Grid() - for y, l in enumerate(lines): - for x, v in enumerate(map(int, l)): - g.set(Coordinate(x, y), v) - if not multiply: - continue - - for x2 in range(5): - for y2 in range(5): - if x2 == y2 == 0: - continue - nv = 1 + (v + x2 + y2 - 1) % 9 - - g.set(Coordinate(size * x2 + x, size * y2 + y), nv) - - return g - - class Day(AOCDay): inputs = [ [ @@ -36,13 +16,32 @@ class Day(AOCDay): ] ] + def getGrid(self, multiply: bool = False) -> Grid: + size = len(self.getInput()) + g = Grid() + for y, l in enumerate(self.getInput()): + for x, v in enumerate(map(int, l)): + g.set(Coordinate(x, y), v) + if not multiply: + continue + + for x2 in range(5): + for y2 in range(5): + if x2 == y2 == 0: + continue + nv = 1 + (v + x2 + y2 - 1) % 9 + + g.set(Coordinate(size * x2 + x, size * y2 + y), nv) + + return g + def part1(self) -> Any: - grid = getGrid(self.getInput()) + grid = self.getGrid() path = grid.getPath(Coordinate(0, 0), Coordinate(grid.maxX, grid.maxY), includeDiagonal=False, weighted=True) return sum(grid.get(c) for c in path[:-1]) def part2(self) -> Any: - grid = getGrid(self.getInput(), True) + grid = self.getGrid(True) path = grid.getPath(Coordinate(0, 0), Coordinate(grid.maxX, grid.maxY), includeDiagonal=False, weighted=True) return sum(grid.get(c) for c in path[:-1])