Skip to main content
deleted 5 characters in body
Source Link
Ajax1234
  • 7.7k
  • 1
  • 15
  • 28

Python3, 294291 bytes:

y=lambda j,n,m:{'v':(n-1,m),'>':(n,m+1),'<':(n,m-1),'^':(n+1,m)}[j]
f=lambda p,s=(0,0),c=[(0,0)],l=[]:0 if not p else(k:=((r:=y(p[0],*s))in c))+(min(f(j+p[1:],[r,s][k],c+[[s],[]][k],l+[p[0]])for j in g) if (g:=[[''],[u for u in'v<>^'if y(u,*s)not in c]][k]) else f(l.pop()+p[1:],c[-1],[r]+c,l))

A veryrather unelegant solution, but not entirely brutenon-brute force, with basic backgrackingbacktracking.

Try it online!Try it online!

Python3, 294 bytes:

y=lambda j,n,m:{'v':(n-1,m),'>':(n,m+1),'<':(n,m-1),'^':(n+1,m)}[j]
f=lambda p,s=(0,0),c=[(0,0)],l=[]:0 if not p else(k:=((r:=y(p[0],*s))in c))+(min(f(j+p[1:],[r,s][k],c+[[s],[]][k],l+[p[0]])for j in g) if (g:=[[''],[u for u in'v<>^'if y(u,*s)not in c]][k]) else f(l.pop()+p[1:],c[-1],[r]+c,l))

A very unelegant solution, but not entirely brute force, with basic backgracking.

Try it online!

Python3, 291 bytes:

y=lambda j,n,m:{'v':(n-1,m),'>':(n,m+1),'<':(n,m-1),'^':(n+1,m)}[j]
f=lambda p,s=(0,0),c=[(0,0)],l=[]:0 if not p else(k:=((r:=y(p[0],*s))in c))+(min(f(j+p[1:],[r,s][k],c+[[s],[]][k],l+[p[0]])for j in g)if(g:=[[''],[u for u in'v<>^'if y(u,*s)not in c]][k])else f(l.pop()+p[1:],c[-1],[r]+c,l))

A rather unelegant solution, but non-brute force, with basic backtracking.

Try it online!

added 117 characters in body
Source Link
Ajax1234
  • 7.7k
  • 1
  • 15
  • 28

Python3, 231294 bytes:

y=lambda j,n,m:{'v':(n-1,m),'>':(n,m+1),'<':(n,m-1),'^':(n+1,m)}[j]
f=lambda p,s=(0,0),c=[(0,0)],l=[]:0 if not p else(k:=((r:=y(p[0],*s))in c))+min+(min(f(j+p[1:],[r,s][k],c+[[s],[]][k],l+[p[0]])for j in[['']in g) if (g:=[[''],[u for u in'v<>^'if y(u,*s)not in c]][k]) else f(l.pop()+p[1:],c[-1],[r]+c,l))

Try it online! A very unelegant solution, but not entirely brute force, with basic backgracking.

Try it online!

Python3, 231 bytes:

y=lambda j,n,m:{'v':(n-1,m),'>':(n,m+1),'<':(n,m-1),'^':(n+1,m)}[j]
f=lambda p,s=(0,0),c=[(0,0)]:0 if not p else(k:=((r:=y(p[0],*s))in c))+min(f(j+p[1:],[r,s][k],c+[[s],[]][k])for j in[[''],[u for u in'v<>^'if y(u,*s)not in c]][k])

Try it online!

Python3, 294 bytes:

y=lambda j,n,m:{'v':(n-1,m),'>':(n,m+1),'<':(n,m-1),'^':(n+1,m)}[j]
f=lambda p,s=(0,0),c=[(0,0)],l=[]:0 if not p else(k:=((r:=y(p[0],*s))in c))+(min(f(j+p[1:],[r,s][k],c+[[s],[]][k],l+[p[0]])for j in g) if (g:=[[''],[u for u in'v<>^'if y(u,*s)not in c]][k]) else f(l.pop()+p[1:],c[-1],[r]+c,l))

A very unelegant solution, but not entirely brute force, with basic backgracking.

Try it online!

Source Link
Ajax1234
  • 7.7k
  • 1
  • 15
  • 28

Python3, 231 bytes:

y=lambda j,n,m:{'v':(n-1,m),'>':(n,m+1),'<':(n,m-1),'^':(n+1,m)}[j]
f=lambda p,s=(0,0),c=[(0,0)]:0 if not p else(k:=((r:=y(p[0],*s))in c))+min(f(j+p[1:],[r,s][k],c+[[s],[]][k])for j in[[''],[u for u in'v<>^'if y(u,*s)not in c]][k])

Try it online!