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)

View File

@@ -29,7 +29,7 @@ written in Python.
- Day 22: That was bad. Did not know how to choose between dfs/bfs and logic errors.
- Day 23: 10:00
- Day 24: 20:00 ugly - recursive solution would be more elegant
- Day 25:
- Day 25: 9:34
# 2016
@@ -50,7 +50,9 @@ written in Python.
- Day 15: Trial and error. Should use CRT instead.
- Day 16: 14:11
- Day 17: 45:00 didn't follow instructions... focus!
- Day 18:
- Day 18: 9:43
- Day 19:
# 2017