We’re rewarding the question askers & reputations are being recalculated! Read more.

New answers tagged

6

Python 3 + SymPy, 480 455 bytes lambda w,h:len({min(I(D(H(V(x)))for x in o)for H,V,D in Q((lambda p:(w-1-p[0],p[1]),I),(lambda p:(p[0],h-1-p[1]),I),(lambda p:p[::-1],I)))for o in permutations(Q(range(w),range(h)))for s in[[*zip(o,o[1:])]]if any(x+X-w+1|y+Y-h+1for(x,y),(X,Y)in zip(o,o[::-1]))+any(gcd(x-X,y-Y)>1for(x,y),(X,Y)in s)+any((len({*u,*v})>3)*...


1

Julia, 254 bytes Not very golfed, just to keep it going; l=length function f(M,r,s) l(r)<7&&return l(s)>6&&g(M,r,s) h,j=rand([f,g],2) a,c=extrema(r) b=rand(a+1:c-1) M[b,s].=2 h(M,b+1:c,s) j(M,a:b-1,s) end g(M,r,s)=f(M',s,r) n=80 M=fill(1,n,n) f(M,1:n,1:n) for i∈1:n print.(getindex.(" #",M[i,:])) println() end Verification, try it ...


3

Javascript (ES6), 208 bytes Recursive, depth-first search. A breadth-first approach would be probably faster, but less golfy. Input: a multi line string, using 1 for ghosts, 4 for solid objects, 6 for Jimmy and 2 for empty space. f=(s,l=-~s.search` `,j=s.search(6),g=[...s],x=j%l)=>j<l|!g[j+l]|!x|x>l-3||(g[j]=6,![...g].some((a,i)=>a&1&&...


1

Jelly, 94 89 bytes Ø.,U$;N$+®ŒṬ€×8+®ŒṬ¤+&7$Ç€Ẹ &Ɱ8,2ŒṪ€Ḣ©_¥/Ṡ01¦Ạ¡€+ƲṪŒṬḤ+&13$ÑÇFṀ>8Ʋ? |Ø.¦4Z$⁺FṀ=12ƲÇFṀ>8Ʋ? Try it online! A full program that takes an integer matrix as its argument, with 0 as space, 1 as wall, 2 as ghost and 8 as Jimmy. Returns 1 for escape and 0 for no escape.


6

Python 2, 199 bytes def g(I,G,W,w,h): u,v=I.real,I.imag;R=1-(w-1>u>0<v<h-1);H=[z+cmp(u,z.real)+cmp(v,z.imag)*(u==z.real)*1jfor z in G] for d in(R<1>(I in H+W))*range(4):J=I+1j**d;R|=(J in W)<g(J,H,W+[I],w,h) return R Try it online! Rewrite of Chas Brown's solution using complex numbers. All the coordinates are represented as a ...


8

Python 2, 236 235 227 212 bytes def f((x,y),G,M,U=[]): R=1-(len(M[0])-1>x>0<y<len(M)-1);H=[(u+cmp(x,u),v+cmp(y,v)*(u==x))for u,v in G];d,e=0,1 for _ in' '*4*(R<1>((x,y)in H)):J=z,w=x+d,y+e;d,e=-e,d;R|=M[w][z]>(J in U)<f(J,H,M,U+[J]) return R Try it online! -15 bytes thx to Bubbler As input, takes a tuple (x,y) as jimmy's ...


1

J, 125 bytes p=.((,-)#:1 2)|.!.0] f=.([:(''-:2 0~.@-.~[:,]*+/@p)' '~:])*1-1 e.[:([:,@(,]+1=(2|#\)*/4|#\@|:)([:>./]*"2 p)^:_)_(<0 0)}' '~:] Try it online! Just a first draft, but all test cases are passing. Will attempt to golf and add explanation this week.


5

Jelly, 34 bytes Ø1©ȧn⁶µŒṪạ®SỊƊƇḢ©Wḟ@ƲƬṖṪ<3Ȧȧm2m€4Ȧ A monadic Link accepting a list of lists of characters (the lines) which yields 1 for a Hamiltonian or 0 otherwise. Uses the layout in the question (any non-space chars as path parts will work). Try it online! Or see the test-suite. How? Starts at the top-left, [1,1], and finds a neighbouring non-...


3

Jelly, 54 bytes n⁶,Z$µØ0jm2N,¹ƇɗƝ)m2)Z×LƊ€2¦Ẏ€;"/µØ-ḟN}Ḣɗ,+¥ƭƒịṪ¥¥ƬL=L Try it online! A monadic link which takes a list of strings as input and returns a boolean. The strings use the format of the question but omitting every other character (so half the width). Full explanation to follow.


5

Charcoal, 63 55 46 bytes AM↗ W⁼№KV#¹«✳⊗⁻¹⌕KV#¦ ¿¬∨﹪ⅈ⁴﹪ⅉ²⊞υω»¿‹LυXN²⎚«⎚¹ Try it online! Link is to verbose version of code. Edit: Saved 8 bytes by using a more cumbersome but still acceptable input format. Saved 9 bytes by requiring the path only uses #s. Explanation: A Copy the path to the canvas. M↗ Print a space one square in from the bottom-left ...


4

JavaScript (ES7),  126 124  122 bytes Takes input as (n)(matrix). Returns a Boolean value. n=>g=(m,X=k=0,Y=0,d=0)=>m.map((r,y)=>r.map((c,x)=>++c|(x-X)**2+(y-Y)**2-1||g(m,x,r[++d,x%4|y%2||k++,x]=y)))|d>1?k=g:k==n*n Try it online! How? Starting at \$(0,0)\$, we flood-fill the path and increment a counter \$k\$ each time we go ...


Top 50 recent answers are included