This commit is contained in:
basicbonobo 2024-01-05 00:53:38 +01:00
parent 0899745dcf
commit 4ae3c22591
2 changed files with 51 additions and 0 deletions

40
einundzwanzig.py Executable file
View File

@ -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)))

11
test21 Normal file
View File

@ -0,0 +1,11 @@
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........