41 lines
988 B
Python
41 lines
988 B
Python
from aoc import AOCDay
|
|
from coordinate import Coordinate
|
|
from 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]
|
|
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)
|
|
|