21a
This commit is contained in:
parent
0899745dcf
commit
4ae3c22591
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from useful import *
|
||||
|
||||
target = 64 if not len(sys.argv) > 1 else int(sys.argv[1])
|
||||
|
||||
DIRS = (-1, 0), (1, 0), (0, -1), (0, 1)
|
||||
|
||||
def add(a, b):
|
||||
return tuple(map(lambda u, v: u + v, a, b))
|
||||
|
||||
def cango(pos):
|
||||
global w, h
|
||||
return pos[0] >= 0 and pos[1] >= 0 and pos[0] < h and pos[1] < w and \
|
||||
kart[pos] != '#'
|
||||
|
||||
def solve(kart, pos, dist):
|
||||
global dmap, target, q
|
||||
if dist > target:
|
||||
return
|
||||
if dist >= dmap[pos]:
|
||||
return
|
||||
dmap[pos] = dist
|
||||
for d in DIRS:
|
||||
nextpos = add(pos, d)
|
||||
if cango(nextpos):
|
||||
q += [(kart, nextpos, dist + 1), ]
|
||||
|
||||
kart = np.asarray([[c for c in l] for l in lines(open(0))])
|
||||
h, w = kart.shape
|
||||
dmap = np.ones((h, w), dtype=int) * 666
|
||||
beg = next(zip(*np.where(kart == 'S')))
|
||||
q = deque()
|
||||
|
||||
q += [(kart, beg, 0)]
|
||||
|
||||
while q:
|
||||
solve(*q.popleft())
|
||||
|
||||
print(np.sum(np.vectorize(lambda x: x <= target and x % 2 == 0)(dmap)))
|
Loading…
Reference in New Issue