aoc2021/day05.py

41 lines
1019 B
Python

from tools.aoc import AOCDay
from tools.coordinate import Coordinate
from tools.grid import Grid
from typing import Any
def buildLineGrid(lines: list[list[str]], diagonals: bool = False) -> Grid:
grid = Grid(0)
for line in lines:
start = Coordinate(*map(int, line[0].split(",")))
end = Coordinate(*map(int, line[1].split(",")))
if not diagonals and start.x != end.x and start.y != end.y:
continue
for c in start.getLineTo(end):
grid.add(c)
return grid
def getCrossCount(grid: Grid) -> int:
count = 0
for x in grid.getActiveCells():
count += grid.get(x) > 1
return count
class Day(AOCDay):
test_solutions_p1 = [5, 6710]
test_solutions_p2 = [12, 20121]
def part1(self) -> Any:
grid = buildLineGrid(self.getInputAsArraySplit(" -> "))
return getCrossCount(grid)
def part2(self) -> Any:
grid = buildLineGrid(self.getInputAsArraySplit(" -> "), True)
return getCrossCount(grid)