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,29 +192,26 @@ def part2(test_mode=False):
seamonster_line_3 = re.compile(r".#..#..#..#..#..#...")
seamonster_hashcount = 15 # amount of '#' in the sea monster
seamonster_count = 0
for _ in range(2):
for _ in range(2):
for _ in range(4):
for x in range(len(borderless_image) - 2):
if s_2 := re.split(seamonster_line_2, borderless_image[x+1]):
if s_3 := re.split(seamonster_line_3, borderless_image[x+2]):
# yes, this is somewhat optimistic, but it results in the correct answer
if len(s_2) > 1 and len(s_2) == len(s_3):
seamonster_count += len(s_2) - 1
if seamonster_count == 0:
borderless_image = rotateRight(borderless_image)
else:
break
if seamonster_count == 0:
borderless_image = flipHorizontally(borderless_image)
else:
break
transformations = [
flipHorizontally,
flipVertically,
flipHorizontally,
rotateRight,
flipHorizontally,
flipVertically,
flipHorizontally,
]
transformation_counter = 0
while seamonster_count == 0:
for x in range(len(borderless_image) - 2):
if s_2 := re.split(seamonster_line_2, borderless_image[x+1]):
if s_3 := re.split(seamonster_line_3, borderless_image[x+2]):
# yes, this is somewhat optimistic, but it results in the correct answer
if len(s_2) > 1 and len(s_2) == len(s_3):
seamonster_count += len(s_2) - 1
if seamonster_count == 0:
borderless_image = flipVertically(borderless_image)
else:
break
borderless_image = transformations[transformation_counter](borderless_image)
transformation_counter += 1
return full_hash_count - seamonster_count * seamonster_hashcount