2024-12-18 05:19:25 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
import astar
|
|
|
|
|
|
|
|
def parse(file):
|
|
|
|
return [
|
|
|
|
tuple([int(x) for x in line.strip().split(',')])
|
|
|
|
for line in file
|
|
|
|
]
|
|
|
|
|
2024-12-18 05:28:30 +00:00
|
|
|
def solve(file, N=71, T=1024):
|
2024-12-18 05:19:25 +00:00
|
|
|
falling = parse(file)
|
|
|
|
inf = float('inf')
|
2024-12-18 05:35:28 +00:00
|
|
|
map = defaultdict(lambda: 1)
|
2024-12-18 05:28:30 +00:00
|
|
|
for i,p in enumerate(falling):
|
2024-12-18 05:35:28 +00:00
|
|
|
if i > T:
|
|
|
|
break
|
|
|
|
map[p] = 0
|
|
|
|
sol = _solve(map, N)
|
2024-12-18 05:28:30 +00:00
|
|
|
print(sol)
|
|
|
|
print(len(sol[-1])-1)
|
|
|
|
|
2024-12-18 05:35:28 +00:00
|
|
|
def _solve(map, N):
|
|
|
|
start = (0,0) # x,y
|
|
|
|
end = N-1, N-1
|
2024-12-18 05:19:25 +00:00
|
|
|
|
|
|
|
def neighbors(n):
|
2024-12-18 05:28:30 +00:00
|
|
|
x,y = n
|
|
|
|
def push(x,y):
|
2024-12-18 05:19:25 +00:00
|
|
|
if 0 <= x < N and 0 <= y < N:
|
2024-12-18 05:35:28 +00:00
|
|
|
if map[x,y]:
|
2024-12-18 05:28:30 +00:00
|
|
|
out.append((1,(x,y)))
|
2024-12-18 05:19:25 +00:00
|
|
|
out = []
|
2024-12-18 05:28:30 +00:00
|
|
|
push(x+1,y)
|
|
|
|
push(x,y+1)
|
|
|
|
push(x-1,y)
|
|
|
|
push(x,y-1)
|
2024-12-18 05:19:25 +00:00
|
|
|
return out
|
|
|
|
|
2024-12-18 05:28:30 +00:00
|
|
|
def heuristic(n):
|
|
|
|
return abs(end[0] - n[0]) + abs(end[1] - n[1])
|
|
|
|
|
|
|
|
return astar.search(start, end, neighbors, heuristic)
|
|
|
|
|
|
|
|
import math
|
2024-12-18 05:35:28 +00:00
|
|
|
def solve2(file, N):
|
2024-12-18 05:28:30 +00:00
|
|
|
falling = parse(file)
|
2024-12-18 05:35:28 +00:00
|
|
|
map = defaultdict(lambda: 1)
|
2024-12-18 05:28:30 +00:00
|
|
|
for i,p in enumerate(falling):
|
2024-12-18 05:35:28 +00:00
|
|
|
map[p] = 0
|
|
|
|
sol = _solve(map, N)
|
2024-12-18 05:28:30 +00:00
|
|
|
if math.isinf(sol[0]):
|
|
|
|
print(p)
|
|
|
|
break
|
2024-12-18 05:19:25 +00:00
|
|
|
|
|
|
|
solve(open("sample1.in"), N=7, T=12)
|
2024-12-18 05:35:28 +00:00
|
|
|
solve2(open("sample1.in"), N=7)
|
|
|
|
print()
|
2024-12-18 05:19:25 +00:00
|
|
|
solve(open("input"), N=71, T=1024)
|
2024-12-18 05:35:28 +00:00
|
|
|
solve2(open("input"), N=71)
|