aoc2021/day15.py
2022-01-14 12:44:09 +01:00

52 lines
1.5 KiB
Python

from tools.aoc import AOCDay
from tools.coordinate import Coordinate
from tools.grid import Grid
from typing import Any, List
class Day(AOCDay):
inputs = [
[
(40, "test_input15"),
(386, "input15")
],
[
(315, "test_input15"),
(2806, "input15")
]
]
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 = 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 = 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])
if __name__ == '__main__':
day = Day(2021, 15)
day.run(verbose=True)