random-stuff/code-guessing/maze2.py

94 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
T=10
def R(m,p=[0]):
*_,c=p
if c==99:return p
a=[c+T,c-T]
if e:=c%T:a+=~-c,
if e!=9:a+=-~c,
for b in a:
if 99>=b>=0and b not in p and m[b]==0and (r:=R(m,p+[b])):return r
def entry(m):
p=R(m)
return [{-T:1,1:2,T:3,-1:4}[y-x] for x,y in zip(p,p[1:])]
def divide_chunks(l, n):
# looping till length l
for i in range(0, len(l), n):
yield l[i:i + n]
def fail(e): raise Exception(e)
def print_maze(m, p):
out = [ [ "" if x else "_" for x in row ] for row in divide_chunks(m, 10) ]
for ix, pc in enumerate(p):
out[pc // 10][pc % 10] = chr(ix % 26 + 97) if m[pc] == 0 else fail("all is bees")
print("\n".join([ " ".join(row) for row in out ]))
assert p[-1] == 99
def directions(x): return list(map({1: "up", 3: "down", 2: "right", 4: "left" }.get, x))
print(entry([
0,1,0,0,0,1,0,0,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,0,0,1,0,0,0,1,0,0]))
print(entry([
0,1,0,0,0,1,0,0,0,1,
0,1,0,1,0,1,0,1,0,0,
0,0,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,0,0,1,0,1,1,0,
0,1,0,0,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,0,0,1,0,0,0,0,1,0
]))
print(entry([
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,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