53 lines
1.2 KiB
Python
53 lines
1.2 KiB
Python
from tools.aoc import AOCDay
|
|
from tools.coordinate import Coordinate
|
|
from tools.grid import Grid
|
|
from typing import Any, List
|
|
|
|
|
|
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):
|
|
inputs = [
|
|
[
|
|
(5, "test_input05"),
|
|
(6710, "input05")
|
|
],
|
|
[
|
|
(12, "test_input05"),
|
|
(20121, "input05")
|
|
]
|
|
]
|
|
|
|
def part1(self) -> Any:
|
|
grid = buildLineGrid(self.getInputAsArraySplit(" -> "))
|
|
return getCrossCount(grid)
|
|
|
|
def part2(self) -> Any:
|
|
grid = buildLineGrid(self.getInputAsArraySplit(" -> "), True)
|
|
return getCrossCount(grid)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
day = Day(5)
|
|
day.run(verbose=True)
|