fix transformations

This commit is contained in:
Stefan Harmuth 2022-12-04 10:57:28 +01:00
parent 353ff51411
commit ab05a1a770
2 changed files with 52 additions and 4 deletions

View File

@ -26,6 +26,8 @@ class AOCDay:
self.day = day self.day = day
self.year = year self.year = year
self.part_func = [self.part1, self.part2] self.part_func = [self.part1, self.part2]
self._current_test_file = None
self._current_test_solution = None
def part1(self) -> Any: def part1(self) -> Any:
raise NotImplementedError() 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): def run_part(self, part: int, verbose: bool = False, measure_runtime: bool = False, timeit_number: int = 50):
case_count = 0 case_count = 0
for solution, input_file in self.inputs[part]: for solution, input_file in self.inputs[part]:
self._current_test_solution, self._current_test_file = solution, input_file
exec_time = None exec_time = None
answer = None answer = None
self._load_input(input_file) self._load_input(input_file)
@ -51,7 +54,7 @@ class AOCDay:
if solution is None: if solution is None:
print_solution(self.day, part + 1, answer, solution, case_count, exec_time) 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) self._submit(part + 1, answer)
else: else:
if verbose or answer != solution: if verbose or answer != solution:

View File

@ -246,25 +246,31 @@ class Grid:
raise ValueError("Operation not possible in 2D space", mode) raise ValueError("Operation not possible in 2D space", mode)
coords = self.__grid 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: 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(): for c, v in coords.items():
self.set(Coordinate(c.x, -c.z, c.y), v) self.set(Coordinate(c.x, -c.z, c.y), v)
elif mode == GridTransformation.ROTATE_Y: 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(): for c, v in coords.items():
self.set(Coordinate(-c.z, c.y, c.x), v) self.set(Coordinate(-c.z, c.y, c.x), v)
elif mode == GridTransformation.ROTATE_Z: 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(): 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: 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(): for c, v in coords.items():
self.set(Coordinate(c.x, c.z, -c.y), v) self.set(Coordinate(c.x, c.z, -c.y), v)
elif mode == GridTransformation.COUNTER_ROTATE_Y: 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(): for c, v in coords.items():
self.set(Coordinate(c.z, c.y, -c.x), v) self.set(Coordinate(c.z, c.y, -c.x), v)
elif mode == GridTransformation.COUNTER_ROTATE_Z: 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(): 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: elif mode == GridTransformation.FLIP_X:
for c, v in coords.items(): for c, v in coords.items():
self.set(Coordinate(-c.x, c.y, c.z), v) self.set(Coordinate(-c.x, c.y, c.z), v)
@ -329,6 +335,27 @@ class Grid:
return pathCoords 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 = " "): def print(self, spacer: str = "", true_char: str = None, false_char: str = " "):
for y in range(self.minY, self.maxY + 1): for y in range(self.minY, self.maxY + 1):
for x in range(self.minX, self.maxX + 1): for x in range(self.minX, self.maxX + 1):
@ -339,3 +366,21 @@ class Grid:
print(spacer, end="") print(spacer, end="")
print() 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