aoc2021/day13.py

69 lines
1.8 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.getActiveCells(y=y):
grid.add(Coordinate(x.x, targetY), grid.get(x))
grid.set(x, 0)
grid.maxY = axis - 1
elif direction == "x":
for x in range(axis + 1, grid.maxX + 1):
targetX = axis - (x - axis)
for y in grid.getActiveCells(x=x):
grid.add(Coordinate(targetX, y.y), grid.get(y))
grid.set(y, 0)
grid.maxX = axis - 1
class Day(AOCDay):
inputs = [
[
(17, "test_input13"),
(701, "input13")
],
[
("see image above", "input13")
]
]
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))
return len(grid.getActiveCells())
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"
if __name__ == '__main__':
day = Day(13)
day.run(verbose=True)