Skip to main content
Commonmark migration
Source Link

#Python 2: 302 bytes

Python 2: 302 bytes

Explanation

###Explanation II took a different approach than the other answers. General idea: recursively search by finding the shortest path between going straight forward and rotating the board 90 degrees.

#Python 2: 302 bytes

###Explanation I took a different approach than the other answers. General idea: recursively search by finding the shortest path between going straight forward and rotating the board 90 degrees.

Python 2: 302 bytes

Explanation

I took a different approach than the other answers. General idea: recursively search by finding the shortest path between going straight forward and rotating the board 90 degrees.

Source Link

#Python 2: 302 bytes

from re import* r=lambda x:[''.join(_)for _ in zip(*x)][::-1] z=',l)for l in s]' def f(s,b=1,o=0,n=0): exec("s=[sub('(..).(?!$)',r'\\1'%s;"%z+"s=r([sub(' I ','+I+'%s);"%z*4)*b+"t=[sub('I ','@@I'"+z if'I U'in`s`or n>3:return`o%4`+n/4*`s` return min(`o%4`+f(t,0,o,4*(t==s)),f(r(s),0,o+1,n+1),key=len) 

Takes input as an array of strings all with the same length. Prints 0 for right, 1 for down, 2 for left, and 3 for up.

###Explanation I took a different approach than the other answers. General idea: recursively search by finding the shortest path between going straight forward and rotating the board 90 degrees.

from re import* r=lambda x:[''.join(_)for _ in zip(*x)][::-1] #Rotates the board counterclockwise z=',l)for l in s]' #Suffix for hacky exec golfing def f(s,b=1,o=0,n=0): #b is 1 on initial call, 0 on every recursion #o is orientation #n is number of rotations exec("s=[sub('(..).(?!$)',r'\\1'%s;"%z #Squeeze the maze +"s=r([sub(' I ','+I+'%s);"%z*4) #Add walls around the I to keep it in the maze *b #Only if b is 1 +"t=[sub('I ','@@I'"+z #Attempt to move right if'I U'in`s`or n>3:return`o%4`+n/4*`s` #If the I is next to the U, return the orientation #If there were 4 rotations, return a long string return min( #Return the path with the shortest length: `o%4`+f(t,0,o,4*(t==s)), #Moving forward if possible f(r(s),0,o+1,n+1), #Rotating the board key=len) 

Try it online!