59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
from tools.aoc import AOCDay
|
|
from tools.coordinate import Coordinate
|
|
from tools.grid import Grid
|
|
from typing import Any, List
|
|
|
|
|
|
def buildGrid(coords: List[str]) -> Grid:
|
|
grid = Grid(0)
|
|
for c in coords:
|
|
x, y = map(int, c.split(","))
|
|
grid.set(Coordinate(x, y), 1)
|
|
|
|
return grid
|
|
|
|
|
|
def fold(grid: Grid, direction: str, axis: int):
|
|
if direction == "y":
|
|
for y in range(axis + 1, grid.maxY + 1):
|
|
targetY = axis - (y - axis)
|
|
for x in grid.rangeX():
|
|
grid.add(Coordinate(x, targetY), grid.get(Coordinate(x, y)))
|
|
grid.maxY = axis - 1
|
|
elif direction == "x":
|
|
for x in range(axis + 1, grid.maxX + 1):
|
|
targetX = axis - (x - axis)
|
|
for y in grid.rangeY():
|
|
grid.add(Coordinate(targetX, y), grid.get(Coordinate(x, y)))
|
|
grid.maxX = axis - 1
|
|
|
|
|
|
class Day(AOCDay):
|
|
test_solutions_p1 = [17, 701]
|
|
test_solutions_p2 = []
|
|
|
|
def part1(self) -> Any:
|
|
coords, folds = self.getMultiLineInputAsArray()
|
|
grid = buildGrid(coords)
|
|
|
|
fold_1 = folds[0]
|
|
direction, axis = fold_1.split()[-1].split("=")
|
|
fold(grid, direction, int(axis))
|
|
onCount = 0
|
|
for x in grid.rangeX():
|
|
for y in grid.rangeY():
|
|
onCount += grid.get(Coordinate(x, y)) > 0
|
|
|
|
return onCount
|
|
|
|
def part2(self) -> Any:
|
|
coords, folds = self.getMultiLineInputAsArray()
|
|
grid = buildGrid(coords)
|
|
|
|
for thisFold in folds:
|
|
direction, axis = thisFold.split()[-1].split("=")
|
|
fold(grid, direction, int(axis))
|
|
|
|
grid.print(true_char='#')
|
|
return "see image above"
|