2023 Dec 17th
This commit is contained in:
parent
f1ac559664
commit
c056ba9a22
1 changed files with 34 additions and 0 deletions
34
Python/2023/17/main.py
Normal file
34
Python/2023/17/main.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
from heapq import heappush, heappop
|
||||||
|
|
||||||
|
input = open("input", 'r')
|
||||||
|
|
||||||
|
d = [[int(num) for num in line.rstrip()] for line in input]
|
||||||
|
hgt = len(d)
|
||||||
|
wdt = len(d[0])
|
||||||
|
|
||||||
|
def dkstr(mn=1,mx=3):
|
||||||
|
s = set()
|
||||||
|
q = [(0,0,0,0,0)]
|
||||||
|
while q:
|
||||||
|
h,x,y,dx,dy = heappop(q)
|
||||||
|
if (x,y,dx,dy) in s:
|
||||||
|
continue
|
||||||
|
if (x,y) == (wdt-1,hgt-1):
|
||||||
|
return h
|
||||||
|
s.add((x,y,dx,dy))
|
||||||
|
for ndx, ndy in [(0,1),(0,-1),(1,0),(-1,0)]:
|
||||||
|
if (ndx, ndy) == (dx,dy) or (ndx,ndy) == (-dx,-dy):
|
||||||
|
continue
|
||||||
|
nx, ny, nh = x, y, h
|
||||||
|
for i in range(1,mx+1):
|
||||||
|
nx += ndx
|
||||||
|
ny += ndy
|
||||||
|
if (nx < 0 or nx >= wdt) or (ny < 0 or ny >= hgt):
|
||||||
|
break
|
||||||
|
nh += d[ny][nx]
|
||||||
|
if i >= mn:
|
||||||
|
heappush(q, (nh,nx,ny,ndx,ndy))
|
||||||
|
return -1
|
||||||
|
|
||||||
|
print(dkstr())
|
||||||
|
print(dkstr(4,10))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue