day13: no need to join, just to split again afterwards ...
This commit is contained in:
parent
eb62a58f20
commit
1fd5b27944
23
day13.py
23
day13.py
@ -1,13 +1,12 @@
|
||||
from tools.aoc import AOCDay
|
||||
from typing import Any
|
||||
|
||||
from tools.coordinate import Coordinate
|
||||
from tools.grid import Grid
|
||||
from typing import Any, List
|
||||
|
||||
|
||||
def buildGrid(coords: str) -> Grid:
|
||||
def buildGrid(coords: List[str]) -> Grid:
|
||||
grid = Grid(0)
|
||||
for c in coords.split(";"):
|
||||
for c in coords:
|
||||
x, y = map(int, c.split(","))
|
||||
grid.set(Coordinate(x, y), 1)
|
||||
|
||||
@ -18,13 +17,13 @@ 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 range(grid.minX, grid.maxX + 1):
|
||||
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 range(grid.minY, grid.maxY + 1):
|
||||
for y in grid.rangeY():
|
||||
grid.add(Coordinate(targetX, y), grid.get(Coordinate(x, y)))
|
||||
grid.maxX = axis - 1
|
||||
|
||||
@ -34,24 +33,24 @@ class Day(AOCDay):
|
||||
test_solutions_p2 = []
|
||||
|
||||
def part1(self) -> Any:
|
||||
coords, folds = self.getMultiLineInputAsArray(join_char=";")
|
||||
coords, folds = self.getMultiLineInputAsArray()
|
||||
grid = buildGrid(coords)
|
||||
|
||||
fold_1 = folds.split(";")[0]
|
||||
fold_1 = folds[0]
|
||||
direction, axis = fold_1.split()[-1].split("=")
|
||||
fold(grid, direction, int(axis))
|
||||
onCount = 0
|
||||
for x in range(grid.minX, grid.maxX + 1):
|
||||
for y in range(grid.minY, grid.maxY + 1):
|
||||
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(join_char=";")
|
||||
coords, folds = self.getMultiLineInputAsArray()
|
||||
grid = buildGrid(coords)
|
||||
|
||||
for thisFold in folds.split(";"):
|
||||
for thisFold in folds:
|
||||
direction, axis = thisFold.split()[-1].split("=")
|
||||
fold(grid, direction, int(axis))
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user