day 18 cleanup
parent
cc853a4dcd
commit
e57c5ecfd1
33
day18/sol.py
33
day18/sol.py
|
@ -10,22 +10,24 @@ def parse(file):
|
|||
def solve(file, N=71, T=1024):
|
||||
falling = parse(file)
|
||||
inf = float('inf')
|
||||
map = [[0]*N for _ in range(N)]
|
||||
map = defaultdict(lambda: inf)
|
||||
map = defaultdict(lambda: 1)
|
||||
for i,p in enumerate(falling):
|
||||
map[p] = i
|
||||
sol = _solve(map, N, T)
|
||||
if i > T:
|
||||
break
|
||||
map[p] = 0
|
||||
sol = _solve(map, N)
|
||||
print(sol)
|
||||
print(len(sol[-1])-1)
|
||||
|
||||
def _solve(map, N=71, T=1024):
|
||||
start = (0,0) # x,y,t
|
||||
def _solve(map, N):
|
||||
start = (0,0) # x,y
|
||||
end = N-1, N-1
|
||||
|
||||
def neighbors(n):
|
||||
x,y = n
|
||||
def push(x,y):
|
||||
if 0 <= x < N and 0 <= y < N:
|
||||
if T <= map[x,y]:
|
||||
if map[x,y]:
|
||||
out.append((1,(x,y)))
|
||||
out = []
|
||||
push(x+1,y)
|
||||
|
@ -34,27 +36,24 @@ def _solve(map, N=71, T=1024):
|
|||
push(x,y-1)
|
||||
return out
|
||||
|
||||
end = N-1, N-1
|
||||
|
||||
def heuristic(n):
|
||||
return abs(end[0] - n[0]) + abs(end[1] - n[1])
|
||||
|
||||
return astar.search(start, end, neighbors, heuristic)
|
||||
|
||||
import math
|
||||
def solve2(file, N=71, T=1024):
|
||||
def solve2(file, N):
|
||||
falling = parse(file)
|
||||
inf = float('inf')
|
||||
map = [[0]*N for _ in range(N)]
|
||||
map = defaultdict(lambda: inf)
|
||||
map = defaultdict(lambda: 1)
|
||||
for i,p in enumerate(falling):
|
||||
map[p] = i
|
||||
sol = _solve(map, N, i+1)
|
||||
map[p] = 0
|
||||
sol = _solve(map, N)
|
||||
if math.isinf(sol[0]):
|
||||
print(p)
|
||||
break
|
||||
|
||||
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)
|
||||
solve2(open("input"), N=71, T=1024)
|
||||
solve2(open("input"), N=71)
|
||||
|
|
Loading…
Reference in New Issue