From 1fd5b2794495e746af036051eb98b0ca7a1e388e Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Mon, 13 Dec 2021 07:03:00 +0100 Subject: [PATCH] day13: no need to join, just to split again afterwards ... --- day13.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/day13.py b/day13.py index 4cdb6ed..1e6d014 100644 --- a/day13.py +++ b/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))