Created
December 23, 2021 15:18
-
-
Save kaathewise/b74d4938b6f3d8495f1802e72a8e5865 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from heapq import heappush, heappop | |
from itertools import takewhile,product,count | |
(D:=4,x:=((*[0]*11,),(1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4)),s:=((*[0]*11,),(4,4,4,3),(4,3,2,1),(2,2,1,2),(1,1,3,3)),d:={s:0},q:=[(0,s)],print(next(filter(None,(len(q)==0 or (t:=heappop(q)) and (v:=t[1]) and d[v]==t[0] and (v==x and d[x] or (f:={(i,j) for i in range(11) for j in takewhile(lambda x: not v[0][x],range(i,11))}) and (p:=[*product(range(1,5),(0,1,3,5,7,9,10))]) and (g:=[0]+[len(v[i])==0 or max(v[i])==min(v[i])==i for i in range(1,5)]) and any((n not in d or d[n]>nd) and (d.update({n:nd}) or heappush(q,(nd,n))) for nd,n in [(d[v]+(D-len(v[i])+abs(j-2*i))*10**(i-1),((*v[0][:j],0,*v[0][j+1:]),*v[1:i],(i,*v[i]),*v[i+1:])) for i,j in p if g[i] and v[0][j]==i and (j<2*i and (j+1,2*i) or (2*i,j-1)) in f]+[(d[v]+(D-len(v[i])+1+abs(j-2*i))*10**(v[i][0]-1),((*v[0][:j],v[i][0],*v[0][j+1:]),*v[1:i],(*v[i][1:],),*v[i+1:])) for i,j in p if not g[i] and (min(2*i,j),max(2*i,j)) in f]) and 0) for _ in count()))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment