Skip to main content
deleted 8 characters in body
Source Link
Justin Mariner
  • 5k
  • 1
  • 14
  • 18

Uses smls's idea of squishing the maze to make each cell 1x1 and removing double directionsrepeated arrows from the final output.

Uses smls's idea of squishing the maze to make each cell 1x1 and removing double directions from the final output.

Uses smls's idea of squishing the maze to make each cell 1x1 and removing repeated arrows from the output.

added 13 characters in body
Source Link
Justin Mariner
  • 5k
  • 1
  • 14
  • 18

JavaScript (ES6), 366356 bytes

a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m.replace(/(.)\1/g,"$1")):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.map(m=>m.replace(/(.)\1/g,"$1")).sort((a,b)=>a[l]-b[l])[0]) 
a=>( a=a.map(s=>s.filter((_,i)=>!i|i%3)), // squish the maze to 1x1 cells g=([x,y])=>a[y]&&a[y][x], // helper func to get maze value o=[], // resulting movesets c=([x,y], m="", v=[]) => // recursive func to search // takes current point, moves, and visited spots [[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(// for each direction p=[x+j,y+k], !m & (!y | y>a[l="length"]-2) == i%2 | // if first move, prevent moving out of maze v.includes(""+p) || ( // also prevent if already visited g(p)<"!" ? // is this a space? c(p, m+"v>^<"[i], [...v,""+p]) // check this spot recursively : g(p)=="U" ? // if this the end? o.push(m)  // add moves to moveset   m.replace(/(.)\1/g,"$1")) // with double arrows removed  : 0 ) )), a.map((h,i)=>h.map((k,j)=> // find the starting "I" and k=="I" && c([j,i]) // begin recursion at that point )), o.map(m=>m.replace(/(.)\1/g,"$1")) // remove doubled directions .sort((a,b)=>a[l]-b[l])[0]  // get shortest path ) 
f= a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.map(m=>m.replace(/(.)\1/g,"$1")).sort((a,b)=>a[l]-b[l])[0]) let tests = [" +--+--+--+--+--+--+--+--+--+--+\nI | | |\n + +--+--+--+ + + + +--+ +\n | | | | | |\n +--+--+--+ +--+--+ + + +--+\n | | | | |\n + +--+--+ + +--+--+ +--+ +\n | | | | | |\n +--+ + +--+--+ +--+--+ + +\n | | | | | |\n + +--+--+--+ +--+--+ + + +\n | | | | | |\n +--+ + +--+--+ +--+--+--+--+\n | | | | |\n + + +--+--+--+ +--+ + + +\n | | | | | | | |\n +--+--+ + +--+ + + +--+ +\n | | | | | |\n + +--+--+--+ + + + + +--+\n | | | | U\n +--+--+--+--+--+--+--+--+--+--+"," +--+--+--+--+--+\nI | | |\n + +--+--+ + +\n | | | |\n + +--+ + + +\n | | | | |\n + + +--+ + +\n | | |\n +--+ + +--+--+\n | | U\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," I\n +--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+\n | | | | |\n + + + +--+--+--+ + +--+--+ +--+--+ + +\n | | | | | | | |\n +--+--+--+ +--+ + +--+--+--+--+ +--+--+--+\n | | | | | | | | |\n + + + + + +--+ + + + +--+--+ +--+ +\n | | | | | | | |\n + +--+--+--+ +--+--+ + +--+ +--+--+ +--+\n | | | | | | | | |\n + +--+ + +--+ +--+--+ +--+--+ + +--+ +\n | | | | | | |\n + + +--+--+--+--+ + +--+--+--+ +--+ +--+\n | | | | | | | |\n +--+--+--+ + +--+--+ +--+ + +--+ +--+ +\n | | | | | | | |\n + +--+--+--+--+ + + +--+--+--+ + + + +\n | | | | | | | | | |\n +--+ + + + + + +--+--+ + + +--+ + +\n | | | | | | | | | |\n +--+ +--+--+ + + + +--+--+--+ + + + +\n | | | | | | | | | |\n + +--+ +--+--+ + +--+--+ + +--+ + + +\n | | | | | | | | | |\n +--+--+--+ + + +--+ + +--+--+ +--+ + +\n | | | | | | | |\n + + +--+--+--+--+ +--+--+ +--+--+ +--+ +\n | | | | | |\n + + + +--+--+--+--+--+--+--+--+ +--+ +--+\n | | | |\n +--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+\n U"," +--+--+--+--+--+\nI |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | U\n +--+--+--+--+--+"," I U\n + + +--+--+--+\n | | | |\n + +--+--+ + +\n | | | |\n +--+ + +--+ +\n | | | |\n + +--+ + + +\n | | |\n +--+ +--+--+ +\n | | |\n +--+--+--+--+--+"," +--+--+--+--+--+\n | | |\n + +--+ +--+ +\nI | | | |\n + + +--+ + +\nU | | | |\n +--+--+ + + +\n | | | |\n + +--+--+--+ +\n | \n +--+--+--+--+--+"]; S.innerHTML="<option>Tests"+Array(tests.length-1).fill().map((_,i)=>"<option>"+(i+1)).join``;
<select id=S oninput="I.value=S.selectedIndex?tests[S.value]:''"></select><br> <textarea id=I rows=20 cols=50></textarea><br><button onclick="O.innerHTML=f(I.value.split`\n`.map(x=>[...x]))">Run</button> <pre id=O>
f= a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m.replace(/(.)\1/g,"$1")):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.sort((a,b)=>a[l]-b[l])[0]) let tests = [" +--+--+--+--+--+--+--+--+--+--+\nI | | |\n + +--+--+--+ + + + +--+ +\n | | | | | |\n +--+--+--+ +--+--+ + + +--+\n | | | | |\n + +--+--+ + +--+--+ +--+ +\n | | | | | |\n +--+ + +--+--+ +--+--+ + +\n | | | | | |\n + +--+--+--+ +--+--+ + + +\n | | | | | |\n +--+ + +--+--+ +--+--+--+--+\n | | | | |\n + + +--+--+--+ +--+ + + +\n | | | | | | | |\n +--+--+ + +--+ + + +--+ +\n | | | | | |\n + +--+--+--+ + + + + +--+\n | | | | U\n +--+--+--+--+--+--+--+--+--+--+"," +--+--+--+--+--+\nI | | |\n + +--+--+ + +\n | | | |\n + +--+ + + +\n | | | | |\n + + +--+ + +\n | | |\n +--+ + +--+--+\n | | U\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," I\n +--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+\n | | | | |\n + + + +--+--+--+ + +--+--+ +--+--+ + +\n | | | | | | | |\n +--+--+--+ +--+ + +--+--+--+--+ +--+--+--+\n | | | | | | | | |\n + + + + + +--+ + + + +--+--+ +--+ +\n | | | | | | | |\n + +--+--+--+ +--+--+ + +--+ +--+--+ +--+\n | | | | | | | | |\n + +--+ + +--+ +--+--+ +--+--+ + +--+ +\n | | | | | | |\n + + +--+--+--+--+ + +--+--+--+ +--+ +--+\n | | | | | | | |\n +--+--+--+ + +--+--+ +--+ + +--+ +--+ +\n | | | | | | | |\n + +--+--+--+--+ + + +--+--+--+ + + + +\n | | | | | | | | | |\n +--+ + + + + + +--+--+ + + +--+ + +\n | | | | | | | | | |\n +--+ +--+--+ + + + +--+--+--+ + + + +\n | | | | | | | | | |\n + +--+ +--+--+ + +--+--+ + +--+ + + +\n | | | | | | | | | |\n +--+--+--+ + + +--+ + +--+--+ +--+ + +\n | | | | | | | |\n + + +--+--+--+--+ +--+--+ +--+--+ +--+ +\n | | | | | |\n + + + +--+--+--+--+--+--+--+--+ +--+ +--+\n | | | |\n +--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+\n U"," +--+--+--+--+--+\nI |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | U\n +--+--+--+--+--+"," I U\n + + +--+--+--+\n | | | |\n + +--+--+ + +\n | | | |\n +--+ + +--+ +\n | | | |\n + +--+ + + +\n | | |\n +--+ +--+--+ +\n | | |\n +--+--+--+--+--+"," +--+--+--+--+--+\n | | |\n + +--+ +--+ +\nI | | | |\n + + +--+ + +\nU | | | |\n +--+--+ + + +\n | | | |\n + +--+--+--+ +\n | \n +--+--+--+--+--+"]; S.innerHTML="<option>Tests"+Array(tests.length).fill().map((_,i)=>"<option>"+(i+1)).join``;
<select id=S oninput="I.value=S.selectedIndex?tests[S.value-1]:''"></select><br> <textarea id=I rows=20 cols=50></textarea><br><button onclick="O.innerHTML=f(I.value.split`\n`.map(x=>[...x]))">Run</button> <pre id=O>

JavaScript (ES6), 366 bytes

a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.map(m=>m.replace(/(.)\1/g,"$1")).sort((a,b)=>a[l]-b[l])[0]) 
a=>( a=a.map(s=>s.filter((_,i)=>!i|i%3)), // squish the maze to 1x1 cells g=([x,y])=>a[y]&&a[y][x], // helper func to get maze value o=[], // resulting movesets c=([x,y], m="", v=[]) => // recursive func to search // takes current point, moves, and visited spots [[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(// for each direction p=[x+j,y+k], !m & (!y | y>a[l="length"]-2) == i%2 | // if first move, prevent moving out of maze v.includes(""+p) || ( // also prevent if already visited g(p)<"!" ? // is this a space? c(p, m+"v>^<"[i], [...v,""+p]) // check this spot recursively : g(p)=="U" ? // if this the end? o.push(m) // add moves to moveset : 0 ) )), a.map((h,i)=>h.map((k,j)=> // find the starting "I" and k=="I" && c([j,i]) // begin recursion at that point )), o.map(m=>m.replace(/(.)\1/g,"$1")) // remove doubled directions .sort((a,b)=>a[l]-b[l])[0]  // get shortest path ) 
f= a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.map(m=>m.replace(/(.)\1/g,"$1")).sort((a,b)=>a[l]-b[l])[0]) let tests = [" +--+--+--+--+--+--+--+--+--+--+\nI | | |\n + +--+--+--+ + + + +--+ +\n | | | | | |\n +--+--+--+ +--+--+ + + +--+\n | | | | |\n + +--+--+ + +--+--+ +--+ +\n | | | | | |\n +--+ + +--+--+ +--+--+ + +\n | | | | | |\n + +--+--+--+ +--+--+ + + +\n | | | | | |\n +--+ + +--+--+ +--+--+--+--+\n | | | | |\n + + +--+--+--+ +--+ + + +\n | | | | | | | |\n +--+--+ + +--+ + + +--+ +\n | | | | | |\n + +--+--+--+ + + + + +--+\n | | | | U\n +--+--+--+--+--+--+--+--+--+--+"," +--+--+--+--+--+\nI | | |\n + +--+--+ + +\n | | | |\n + +--+ + + +\n | | | | |\n + + +--+ + +\n | | |\n +--+ + +--+--+\n | | U\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," I\n +--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+\n | | | | |\n + + + +--+--+--+ + +--+--+ +--+--+ + +\n | | | | | | | |\n +--+--+--+ +--+ + +--+--+--+--+ +--+--+--+\n | | | | | | | | |\n + + + + + +--+ + + + +--+--+ +--+ +\n | | | | | | | |\n + +--+--+--+ +--+--+ + +--+ +--+--+ +--+\n | | | | | | | | |\n + +--+ + +--+ +--+--+ +--+--+ + +--+ +\n | | | | | | |\n + + +--+--+--+--+ + +--+--+--+ +--+ +--+\n | | | | | | | |\n +--+--+--+ + +--+--+ +--+ + +--+ +--+ +\n | | | | | | | |\n + +--+--+--+--+ + + +--+--+--+ + + + +\n | | | | | | | | | |\n +--+ + + + + + +--+--+ + + +--+ + +\n | | | | | | | | | |\n +--+ +--+--+ + + + +--+--+--+ + + + +\n | | | | | | | | | |\n + +--+ +--+--+ + +--+--+ + +--+ + + +\n | | | | | | | | | |\n +--+--+--+ + + +--+ + +--+--+ +--+ + +\n | | | | | | | |\n + + +--+--+--+--+ +--+--+ +--+--+ +--+ +\n | | | | | |\n + + + +--+--+--+--+--+--+--+--+ +--+ +--+\n | | | |\n +--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+\n U"," +--+--+--+--+--+\nI |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | U\n +--+--+--+--+--+"," I U\n + + +--+--+--+\n | | | |\n + +--+--+ + +\n | | | |\n +--+ + +--+ +\n | | | |\n + +--+ + + +\n | | |\n +--+ +--+--+ +\n | | |\n +--+--+--+--+--+"," +--+--+--+--+--+\n | | |\n + +--+ +--+ +\nI | | | |\n + + +--+ + +\nU | | | |\n +--+--+ + + +\n | | | |\n + +--+--+--+ +\n | \n +--+--+--+--+--+"]; S.innerHTML="<option>Tests"+Array(tests.length-1).fill().map((_,i)=>"<option>"+(i+1)).join``;
<select id=S oninput="I.value=S.selectedIndex?tests[S.value]:''"></select><br> <textarea id=I rows=20 cols=50></textarea><br><button onclick="O.innerHTML=f(I.value.split`\n`.map(x=>[...x]))">Run</button> <pre id=O>

JavaScript (ES6), 356 bytes

a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m.replace(/(.)\1/g,"$1")):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.sort((a,b)=>a[l]-b[l])[0]) 
a=>( a=a.map(s=>s.filter((_,i)=>!i|i%3)), // squish the maze to 1x1 cells g=([x,y])=>a[y]&&a[y][x], // helper func to get maze value o=[], // resulting movesets c=([x,y], m="", v=[]) => // recursive func to search // takes current point, moves, and visited spots [[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(// for each direction p=[x+j,y+k], !m & (!y | y>a[l="length"]-2) == i%2 | // if first move, prevent moving out of maze v.includes(""+p) || ( // also prevent if already visited g(p)<"!" ? // is this a space? c(p, m+"v>^<"[i], [...v,""+p]) // check this spot recursively : g(p)=="U" ? // if this the end? o.push(  // add moves to moveset   m.replace(/(.)\1/g,"$1")) // with double arrows removed  : 0 ) )), a.map((h,i)=>h.map((k,j)=> // find the starting "I" and k=="I" && c([j,i]) // begin recursion at that point )), o.sort((a,b)=>a[l]-b[l])[0] // get shortest path ) 
f= a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m.replace(/(.)\1/g,"$1")):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.sort((a,b)=>a[l]-b[l])[0]) let tests = [" +--+--+--+--+--+--+--+--+--+--+\nI | | |\n + +--+--+--+ + + + +--+ +\n | | | | | |\n +--+--+--+ +--+--+ + + +--+\n | | | | |\n + +--+--+ + +--+--+ +--+ +\n | | | | | |\n +--+ + +--+--+ +--+--+ + +\n | | | | | |\n + +--+--+--+ +--+--+ + + +\n | | | | | |\n +--+ + +--+--+ +--+--+--+--+\n | | | | |\n + + +--+--+--+ +--+ + + +\n | | | | | | | |\n +--+--+ + +--+ + + +--+ +\n | | | | | |\n + +--+--+--+ + + + + +--+\n | | | | U\n +--+--+--+--+--+--+--+--+--+--+"," +--+--+--+--+--+\nI | | |\n + +--+--+ + +\n | | | |\n + +--+ + + +\n | | | | |\n + + +--+ + +\n | | |\n +--+ + +--+--+\n | | U\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," I\n +--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+\n | | | | |\n + + + +--+--+--+ + +--+--+ +--+--+ + +\n | | | | | | | |\n +--+--+--+ +--+ + +--+--+--+--+ +--+--+--+\n | | | | | | | | |\n + + + + + +--+ + + + +--+--+ +--+ +\n | | | | | | | |\n + +--+--+--+ +--+--+ + +--+ +--+--+ +--+\n | | | | | | | | |\n + +--+ + +--+ +--+--+ +--+--+ + +--+ +\n | | | | | | |\n + + +--+--+--+--+ + +--+--+--+ +--+ +--+\n | | | | | | | |\n +--+--+--+ + +--+--+ +--+ + +--+ +--+ +\n | | | | | | | |\n + +--+--+--+--+ + + +--+--+--+ + + + +\n | | | | | | | | | |\n +--+ + + + + + +--+--+ + + +--+ + +\n | | | | | | | | | |\n +--+ +--+--+ + + + +--+--+--+ + + + +\n | | | | | | | | | |\n + +--+ +--+--+ + +--+--+ + +--+ + + +\n | | | | | | | | | |\n +--+--+--+ + + +--+ + +--+--+ +--+ + +\n | | | | | | | |\n + + +--+--+--+--+ +--+--+ +--+--+ +--+ +\n | | | | | |\n + + + +--+--+--+--+--+--+--+--+ +--+ +--+\n | | | |\n +--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+\n U"," +--+--+--+--+--+\nI |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | U\n +--+--+--+--+--+"," I U\n + + +--+--+--+\n | | | |\n + +--+--+ + +\n | | | |\n +--+ + +--+ +\n | | | |\n + +--+ + + +\n | | |\n +--+ +--+--+ +\n | | |\n +--+--+--+--+--+"," +--+--+--+--+--+\n | | |\n + +--+ +--+ +\nI | | | |\n + + +--+ + +\nU | | | |\n +--+--+ + + +\n | | | |\n + +--+--+--+ +\n | \n +--+--+--+--+--+"]; S.innerHTML="<option>Tests"+Array(tests.length).fill().map((_,i)=>"<option>"+(i+1)).join``;
<select id=S oninput="I.value=S.selectedIndex?tests[S.value-1]:''"></select><br> <textarea id=I rows=20 cols=50></textarea><br><button onclick="O.innerHTML=f(I.value.split`\n`.map(x=>[...x]))">Run</button> <pre id=O>
Source Link
Justin Mariner
  • 5k
  • 1
  • 14
  • 18

JavaScript (ES6), 366 bytes

a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.map(m=>m.replace(/(.)\1/g,"$1")).sort((a,b)=>a[l]-b[l])[0]) 

Takes input as a 2D array of characters. Each line must be left-padded by one space and have no trailing space, no matter where the start/end points are.

Uses smls's idea of squishing the maze to make each cell 1x1 and removing double directions from the final output.

Ungolfed and Explained

a=>( a=a.map(s=>s.filter((_,i)=>!i|i%3)), // squish the maze to 1x1 cells g=([x,y])=>a[y]&&a[y][x], // helper func to get maze value o=[], // resulting movesets c=([x,y], m="", v=[]) => // recursive func to search // takes current point, moves, and visited spots [[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(// for each direction p=[x+j,y+k], !m & (!y | y>a[l="length"]-2) == i%2 | // if first move, prevent moving out of maze v.includes(""+p) || ( // also prevent if already visited g(p)<"!" ? // is this a space? c(p, m+"v>^<"[i], [...v,""+p]) // check this spot recursively : g(p)=="U" ? // if this the end? o.push(m) // add moves to moveset : 0 ) )), a.map((h,i)=>h.map((k,j)=> // find the starting "I" and k=="I" && c([j,i]) // begin recursion at that point )), o.map(m=>m.replace(/(.)\1/g,"$1")) // remove doubled directions .sort((a,b)=>a[l]-b[l])[0] // get shortest path ) 

Test Snippet

f= a=>(a=a.map(s=>s.filter((_,i)=>!i|i%3)),g=([x,y])=>a[y]&&a[y][x],o=[],c=([x,y],m="",v=[])=>[[0,1],[1,0],[0,-1],[-1,0]].map(([j,k],i)=>(p=[x+j,y+k],!m&(!y|y>a[l="length"]-2)==i%2|v.includes(""+p)||(g(p)<"!"?c(p,m+"v>^<"[i],[...v,""+p]):g(p)=="U"?o.push(m):0))),a.map((h,i)=>h.map((k,j)=>k=="I"&&c([j,i]))),o.map(m=>m.replace(/(.)\1/g,"$1")).sort((a,b)=>a[l]-b[l])[0]) let tests = [" +--+--+--+--+--+--+--+--+--+--+\nI | | |\n + +--+--+--+ + + + +--+ +\n | | | | | |\n +--+--+--+ +--+--+ + + +--+\n | | | | |\n + +--+--+ + +--+--+ +--+ +\n | | | | | |\n +--+ + +--+--+ +--+--+ + +\n | | | | | |\n + +--+--+--+ +--+--+ + + +\n | | | | | |\n +--+ + +--+--+ +--+--+--+--+\n | | | | |\n + + +--+--+--+ +--+ + + +\n | | | | | | | |\n +--+--+ + +--+ + + +--+ +\n | | | | | |\n + +--+--+--+ + + + + +--+\n | | | | U\n +--+--+--+--+--+--+--+--+--+--+"," +--+--+--+--+--+\nI | | |\n + +--+--+ + +\n | | | |\n + +--+ + + +\n | | | | |\n + + +--+ + +\n | | |\n +--+ + +--+--+\n | | U\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," +--+--+--+--+--+\nU | |\n + + +--+--+ +\n | | |\n +--+--+ + +--+\n | | |\n + +--+--+--+ +\n | | | |\n + + + + +--+\n | | I\n +--+--+--+--+--+"," I\n +--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+\n | | | | |\n + + + +--+--+--+ + +--+--+ +--+--+ + +\n | | | | | | | |\n +--+--+--+ +--+ + +--+--+--+--+ +--+--+--+\n | | | | | | | | |\n + + + + + +--+ + + + +--+--+ +--+ +\n | | | | | | | |\n + +--+--+--+ +--+--+ + +--+ +--+--+ +--+\n | | | | | | | | |\n + +--+ + +--+ +--+--+ +--+--+ + +--+ +\n | | | | | | |\n + + +--+--+--+--+ + +--+--+--+ +--+ +--+\n | | | | | | | |\n +--+--+--+ + +--+--+ +--+ + +--+ +--+ +\n | | | | | | | |\n + +--+--+--+--+ + + +--+--+--+ + + + +\n | | | | | | | | | |\n +--+ + + + + + +--+--+ + + +--+ + +\n | | | | | | | | | |\n +--+ +--+--+ + + + +--+--+--+ + + + +\n | | | | | | | | | |\n + +--+ +--+--+ + +--+--+ + +--+ + + +\n | | | | | | | | | |\n +--+--+--+ + + +--+ + +--+--+ +--+ + +\n | | | | | | | |\n + + +--+--+--+--+ +--+--+ +--+--+ +--+ +\n | | | | | |\n + + + +--+--+--+--+--+--+--+--+ +--+ +--+\n | | | |\n +--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+\n U"," +--+--+--+--+--+\nI |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | |\n + + + + + +\n | U\n +--+--+--+--+--+"," I U\n + + +--+--+--+\n | | | |\n + +--+--+ + +\n | | | |\n +--+ + +--+ +\n | | | |\n + +--+ + + +\n | | |\n +--+ +--+--+ +\n | | |\n +--+--+--+--+--+"," +--+--+--+--+--+\n | | |\n + +--+ +--+ +\nI | | | |\n + + +--+ + +\nU | | | |\n +--+--+ + + +\n | | | |\n + +--+--+--+ +\n | \n +--+--+--+--+--+"]; S.innerHTML="<option>Tests"+Array(tests.length-1).fill().map((_,i)=>"<option>"+(i+1)).join``;
<select id=S oninput="I.value=S.selectedIndex?tests[S.value]:''"></select><br> <textarea id=I rows=20 cols=50></textarea><br><button onclick="O.innerHTML=f(I.value.split`\n`.map(x=>[...x]))">Run</button> <pre id=O>