day20: limit transformations to the absolute necessary

This commit is contained in:
Stefan Harmuth 2020-12-20 21:38:49 +01:00
parent 91f4d095f1
commit da5fab3c8c

View File

@ -192,9 +192,17 @@ def part2(test_mode=False):
seamonster_line_3 = re.compile(r".#..#..#..#..#..#...") seamonster_line_3 = re.compile(r".#..#..#..#..#..#...")
seamonster_hashcount = 15 # amount of '#' in the sea monster seamonster_hashcount = 15 # amount of '#' in the sea monster
seamonster_count = 0 seamonster_count = 0
for _ in range(2): transformations = [
for _ in range(2): flipHorizontally,
for _ in range(4): flipVertically,
flipHorizontally,
rotateRight,
flipHorizontally,
flipVertically,
flipHorizontally,
]
transformation_counter = 0
while seamonster_count == 0:
for x in range(len(borderless_image) - 2): for x in range(len(borderless_image) - 2):
if s_2 := re.split(seamonster_line_2, borderless_image[x+1]): if s_2 := re.split(seamonster_line_2, borderless_image[x+1]):
if s_3 := re.split(seamonster_line_3, borderless_image[x+2]): if s_3 := re.split(seamonster_line_3, borderless_image[x+2]):
@ -203,18 +211,7 @@ def part2(test_mode=False):
seamonster_count += len(s_2) - 1 seamonster_count += len(s_2) - 1
if seamonster_count == 0: if seamonster_count == 0:
borderless_image = rotateRight(borderless_image) borderless_image = transformations[transformation_counter](borderless_image)
else: transformation_counter += 1
break
if seamonster_count == 0:
borderless_image = flipHorizontally(borderless_image)
else:
break
if seamonster_count == 0:
borderless_image = flipVertically(borderless_image)
else:
break
return full_hash_count - seamonster_count * seamonster_hashcount return full_hash_count - seamonster_count * seamonster_hashcount