day 18 cleanup

main
magical 2024-12-18 05:35:28 +00:00
parent cc853a4dcd
commit e57c5ecfd1
1 changed files with 16 additions and 17 deletions

View File

@ -10,22 +10,24 @@ def parse(file):
def solve(file, N=71, T=1024): def solve(file, N=71, T=1024):
falling = parse(file) falling = parse(file)
inf = float('inf') inf = float('inf')
map = [[0]*N for _ in range(N)] map = defaultdict(lambda: 1)
map = defaultdict(lambda: inf)
for i,p in enumerate(falling): for i,p in enumerate(falling):
map[p] = i if i > T:
sol = _solve(map, N, T) break
map[p] = 0
sol = _solve(map, N)
print(sol) print(sol)
print(len(sol[-1])-1) print(len(sol[-1])-1)
def _solve(map, N=71, T=1024): def _solve(map, N):
start = (0,0) # x,y,t start = (0,0) # x,y
end = N-1, N-1
def neighbors(n): def neighbors(n):
x,y = n x,y = n
def push(x,y): def push(x,y):
if 0 <= x < N and 0 <= y < N: if 0 <= x < N and 0 <= y < N:
if T <= map[x,y]: if map[x,y]:
out.append((1,(x,y))) out.append((1,(x,y)))
out = [] out = []
push(x+1,y) push(x+1,y)
@ -34,27 +36,24 @@ def _solve(map, N=71, T=1024):
push(x,y-1) push(x,y-1)
return out return out
end = N-1, N-1
def heuristic(n): def heuristic(n):
return abs(end[0] - n[0]) + abs(end[1] - n[1]) return abs(end[0] - n[0]) + abs(end[1] - n[1])
return astar.search(start, end, neighbors, heuristic) return astar.search(start, end, neighbors, heuristic)
import math import math
def solve2(file, N=71, T=1024): def solve2(file, N):
falling = parse(file) falling = parse(file)
inf = float('inf') map = defaultdict(lambda: 1)
map = [[0]*N for _ in range(N)]
map = defaultdict(lambda: inf)
for i,p in enumerate(falling): for i,p in enumerate(falling):
map[p] = i map[p] = 0
sol = _solve(map, N, i+1) sol = _solve(map, N)
if math.isinf(sol[0]): if math.isinf(sol[0]):
print(p) print(p)
break break
solve(open("sample1.in"), N=7, T=12) solve(open("sample1.in"), N=7, T=12)
solve2(open("sample1.in"), N=7, T=12) solve2(open("sample1.in"), N=7)
print()
solve(open("input"), N=71, T=1024) solve(open("input"), N=71, T=1024)
solve2(open("input"), N=71, T=1024) solve2(open("input"), N=71)