aoc2021/day15.py
2021-12-15 09:41:39 +01:00

40 lines
1.3 KiB
Python

from tools.aoc import AOCDay
from tools.coordinate import Coordinate
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 multiply:
for x2 in range(5):
for y2 in range(5):
if x2 == 0 and y2 == 0:
continue
nv = v + x2 + y2
if nv > 9:
nv -= 9
g.set(Coordinate(size * x2 + x, size * y2 + y), nv)
return g
class Day(AOCDay):
test_solutions_p1 = [40]
test_solutions_p2 = [315]
def part1(self) -> Any:
grid = getGrid(self.getInput())
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)
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])