day20: limit transformations to the absolute necessary

This commit is contained in:
Stefan Harmuth 2020-12-20 21:34:52 +01:00
parent 4c9e7f2838
commit 91f4d095f1

View File

@ -48,6 +48,10 @@ class Tile:
def flipVertically(self): def flipVertically(self):
self.image = flipVertically(self.image) self.image = flipVertically(self.image)
def printImage(self):
for x in self.image:
print(x)
def rotateRight(image): def rotateRight(image):
return ["".join([x[line_no] for x in reversed(image)]) for line_no in range(len(image))] return ["".join([x[line_no] for x in reversed(image)]) for line_no in range(len(image))]
@ -115,30 +119,26 @@ def part2(test_mode=False):
# first - find correct orientation of corner piece # first - find correct orientation of corner piece
possible_borders = getAllPossibleBorders(tile_dict) possible_borders = getAllPossibleBorders(tile_dict)
transformations = [
tile_dict[corner_piece].flipHorizontally,
tile_dict[corner_piece].flipVertically,
tile_dict[corner_piece].flipHorizontally,
tile_dict[corner_piece].rotateRight,
tile_dict[corner_piece].flipHorizontally,
tile_dict[corner_piece].flipVertically,
tile_dict[corner_piece].flipHorizontally,
]
transformation_counter = 0
found = False found = False
for _ in range(2): while not found:
for _ in range(2): if possible_borders.count(tile_dict[corner_piece].getCurrentBorders()[0]) == 1 \
for _ in range(4): and possible_borders.count(tile_dict[corner_piece].getCurrentBorders()[3]) == 1:
if possible_borders.count(tile_dict[corner_piece].getCurrentBorders()[0]) == 1 \ found = True
and possible_borders.count(tile_dict[corner_piece].getCurrentBorders()[3]) == 1:
found = True
break
tile_dict[corner_piece].rotateRight()
if found:
break
else:
tile_dict[corner_piece].flipHorizontally()
if found:
break
else: else:
tile_dict[corner_piece].flipVertically() transformations[transformation_counter]()
transformation_counter += 1
# flip diagonally to match with example from aoc webpage assert found is True
tile_dict[corner_piece].rotateRight()
tile_dict[corner_piece].flipVertically()
full_image = [] full_image = []
for y in range(borderlen): for y in range(borderlen):
@ -157,28 +157,26 @@ def part2(test_mode=False):
for possible_tile in all_pieces: for possible_tile in all_pieces:
if check_border in tile_dict[possible_tile].getPossibleBorderList(): if check_border in tile_dict[possible_tile].getPossibleBorderList():
# found my piece ... now orientate it correctly # found my piece ... now orientate it correctly
transformations = [
tile_dict[possible_tile].flipHorizontally,
tile_dict[possible_tile].flipVertically,
tile_dict[possible_tile].flipHorizontally,
tile_dict[possible_tile].rotateRight,
tile_dict[possible_tile].flipHorizontally,
tile_dict[possible_tile].flipVertically,
tile_dict[possible_tile].flipHorizontally,
]
transformation_counter = 0
oriented = False oriented = False
for _ in range(2): while not oriented:
for _ in range(2): if (x == 0 and tile_dict[possible_tile].getCurrentBorders()[0] == check_border) \
for _ in range(4): or (x != 0 and tile_dict[possible_tile].getCurrentBorders()[3] == check_border):
if (x == 0 and tile_dict[possible_tile].getCurrentBorders()[0] == check_border)\ oriented = True
or (x != 0 and tile_dict[possible_tile].getCurrentBorders()[3] == check_border): full_image[y].append(possible_tile)
oriented = True all_pieces.remove(possible_tile)
full_image[y].append(possible_tile)
all_pieces.remove(possible_tile)
break
tile_dict[possible_tile].rotateRight()
if oriented:
break
else:
tile_dict[possible_tile].flipVertically()
if oriented:
break
else: else:
tile_dict[possible_tile].flipHorizontally() transformations[transformation_counter]()
transformation_counter += 1
# now that we have the full picture, assemble the borderless full image # now that we have the full picture, assemble the borderless full image
borderless_image = [] borderless_image = []