aoc2021/day05.py
Stefan Harmuth da4b6567a3 day05
2021-12-05 06:47:10 +01:00

43 lines
1.1 KiB
Python

from aoc import AOCDay
from coordinate import Coordinate
from grid import Grid
from tools import compare
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
diff = end - start
for x in range(max(abs(diff.x), abs(diff.y)) + 1):
grid.add(Coordinate(start.x + compare(diff.x, 0) * x, start.y + compare(diff.y, 0) * x))
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]
test_solutions_p2 = [12]
def part1(self) -> Any:
grid = buildLineGrid(self.getInputAsArraySplit(" -> "))
return getCrossCount(grid)
def part2(self) -> Any:
grid = buildLineGrid(self.getInputAsArraySplit(" -> "), True)
return getCrossCount(grid)