Solve 2016 day 18.

This commit is contained in:
2024-05-08 18:58:15 -04:00
parent 4e6a44f67b
commit 3c290b8da3
2 changed files with 36 additions and 2 deletions

32
2016/d18.py Normal file
View File

@@ -0,0 +1,32 @@
from lib import *
row = open(0).read().strip()
def is_trap(s):
assert len(s) == 3
match s:
case "^^.":
return "^"
case ".^^":
return "^"
case "^..":
return "^"
case "..^":
return "^"
case _:
return "."
# 400_000 is still easily bruteforcible. If it was a much larget number, we
# would have to cache the rows till we get a repeated row. We would then
# memorize the number of safe tiles from repeated row to repeated row and jump
# forward by multiples of that amount to reach much higher numbers.
r = 0
for _ in range(400_000):
r += row.count(".")
nrow = is_trap("." + row[:2])
for i in range(1, len(row) - 1):
nrow += is_trap(row[i-1:i+2])
nrow += is_trap(row[-2:] + ".")
row = nrow
print(r)