From ab05a1a7706d358d38a71493af0781ae18278a31 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 4 Dec 2022 10:57:28 +0100 Subject: [PATCH] fix transformations --- tools/aoc.py | 5 ++++- tools/grid.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/tools/aoc.py b/tools/aoc.py index c54aeac..4a35224 100644 --- a/tools/aoc.py +++ b/tools/aoc.py @@ -26,6 +26,8 @@ class AOCDay: self.day = day self.year = year self.part_func = [self.part1, self.part2] + self._current_test_file = None + self._current_test_solution = None def part1(self) -> Any: raise NotImplementedError() @@ -36,6 +38,7 @@ class AOCDay: def run_part(self, part: int, verbose: bool = False, measure_runtime: bool = False, timeit_number: int = 50): case_count = 0 for solution, input_file in self.inputs[part]: + self._current_test_solution, self._current_test_file = solution, input_file exec_time = None answer = None self._load_input(input_file) @@ -51,7 +54,7 @@ class AOCDay: if solution is None: print_solution(self.day, part + 1, answer, solution, case_count, exec_time) - if answer not in {u"", b"", None, b"None", u"None"}: + if answer not in {u"", b"", None, b"None", u"None", 0, '0'}: self._submit(part + 1, answer) else: if verbose or answer != solution: diff --git a/tools/grid.py b/tools/grid.py index 049e549..6c5b53a 100644 --- a/tools/grid.py +++ b/tools/grid.py @@ -246,25 +246,31 @@ class Grid: raise ValueError("Operation not possible in 2D space", mode) coords = self.__grid - self.__grid, self.minX, self.maxX, self.minY, self.maxY, self.minZ, self.maxZ = {}, 0, 0, 0, 0, 0, 0 + self.__grid = {} if mode == GridTransformation.ROTATE_X: + self.minY, self.maxY, self.minZ, self.maxZ = self.minZ, self.maxZ, -self.maxY, -self.minY for c, v in coords.items(): self.set(Coordinate(c.x, -c.z, c.y), v) elif mode == GridTransformation.ROTATE_Y: + self.minX, self.maxX, self.minZ, self.maxZ = -self.maxZ, -self.minZ, self.minX, self.maxX for c, v in coords.items(): self.set(Coordinate(-c.z, c.y, c.x), v) elif mode == GridTransformation.ROTATE_Z: + self.minX, self.maxX, self.minY, self.maxY = -self.maxY, -self.minY, self.minX, self.minY for c, v in coords.items(): - self.set(Coordinate(c.y, -c.x, c.z), v) + self.set(Coordinate(-c.y, c.x, c.z), v) elif mode == GridTransformation.COUNTER_ROTATE_X: + self.minY, self.maxY, self.minZ, self.maxZ = -self.maxZ, -self.minZ, self.minY, self.maxY for c, v in coords.items(): self.set(Coordinate(c.x, c.z, -c.y), v) elif mode == GridTransformation.COUNTER_ROTATE_Y: + self.minX, self.maxX, self.minZ, self.maxZ = self.minZ, self.maxZ, -self.minX, -self.maxX for c, v in coords.items(): self.set(Coordinate(c.z, c.y, -c.x), v) elif mode == GridTransformation.COUNTER_ROTATE_Z: + self.minX, self.maxX, self.minY, self.maxY = self.minY, self.maxY, -self.minX, -self.maxX for c, v in coords.items(): - self.set(Coordinate(-c.y, c.x, c.z), v) + self.set(Coordinate(c.y, -c.x, c.z), v) elif mode == GridTransformation.FLIP_X: for c, v in coords.items(): self.set(Coordinate(-c.x, c.y, c.z), v) @@ -329,6 +335,27 @@ class Grid: return pathCoords + def sub_grid(self, from_x: int, from_y: int, to_x: int, to_y: int) -> 'Grid': + count_x, count_y = 0, 0 + new_grid = Grid() + for x in range(from_x, to_x + 1): + for y in range(from_y, to_y + 1): + new_grid.set(Coordinate(count_x, count_y), self.get(Coordinate(x, y))) + count_y += 1 + count_y = 0 + count_x += 1 + + return new_grid + + def update(self, x: int, y: int, grid: Grid) -> None: + put_x, put_y = x, y + for get_x in grid.rangeX(): + for get_y in grid.rangeY(): + self.set(Coordinate(put_x, put_y), grid.get(Coordinate(get_x, get_y))) + put_y += 1 + put_y = y + put_x += 1 + def print(self, spacer: str = "", true_char: str = None, false_char: str = " "): for y in range(self.minY, self.maxY + 1): for x in range(self.minX, self.maxX + 1): @@ -339,3 +366,21 @@ class Grid: print(spacer, end="") print() + + def __str__(self, true_char: str = '#', false_char: str = "."): + return "/".join( + "".join( + true_char if self.get(Coordinate(x, y)) else false_char + for x in range(self.minX, self.maxX + 1) + ) + for y in range(self.minY, self.maxY + 1) + ) + + @classmethod + def from_str(cls, grid_string: str, true_char: str = '#') -> 'Grid': + ret = cls() + for y, line in enumerate(grid_string.split("/")): + for x, c in enumerate(line): + ret.set(Coordinate(x, y), c == true_char) + + return ret