#JavaScript (ES6), 174 bytes
JavaScript (ES6), 174 bytes
###Commented
Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack ExchangeStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack Internal#JavaScript (ES6), 174 bytes
###Commented
#JavaScript (ES6), 174 bytes
###Commented
#JavaScript (ES6), 175174 bytes
p=>a=>(r=F=([x,y],n,R=a[y],c=R[x])=>R[c===>R[c&(R[x]=4)|n>=r||[-1,0,1,2].map(d=>(g=_=>(k=a[v=Y,Y+=d%2][h=X,X+=~-d%2])||g())(X=x,Y=y)>3?0:k>2?r=-~n:F(k>1?[X,Y]:[h,v],-~n)),x]=c)(p)|r p => a => ( // given the starting position p[] and the matrix a[] r = // r = best result, initialized to a non-numeric value F = ( // F = recursive function taking: [x, y], // (x, y) = current position n, // n = number of shots, initially undefined R = a[y], // R = current row in the matrix c = R[x] // c = value of the current cell ) => // R[ // this will update R[x] once the inner code is executed c ==& (R[x] = 4) | // set the current cell to 4 (water); abort if it was n >= r || // already set to 4 or n is greater than or equal to r [-1, 0, 1, 2].map(d => // otherwise, for each direction d: (g = _ => ( // g = recursive function performing the shot by k = a[ // saving a backup (h, v) of (X, Y) v = Y, Y += d % 2][ // and updating (X, Y) until we reach a cell h = X, X += ~-d % 2]) // whose value k is not ice0 (k != 0ice) || g() // )(X = x, Y = y) // initial call to g() with (X, Y) = (x, y) > 3 ? // if k = 4 (water -> fail): 0 // abort immediately : // else: k > 2 ? // if k = 3 (hole -> success): r = -~n // set r to n + 1 : // else: F( // do a recursive call to F(): k > 1 ? // if k = 2 (sand): [X, Y] // start the next shots from the last cell : // else (wall): [h, v], // start from the last ice cell -~n // increment the number of shots ) // end of recursive call ), x // end of map(); x = actual index used to access R[] ] = c // restore the value of the current cell to c )(p) | r // initial call to F() at the starting position; return r #JavaScript (ES6), 175 bytes
p=>a=>(r=F=([x,y],n,R=a[y],c=R[x])=>R[c==(R[x]=4)|n>=r||[-1,0,1,2].map(d=>(g=_=>(k=a[v=Y,Y+=d%2][h=X,X+=~-d%2])||g())(X=x,Y=y)>3?0:k>2?r=-~n:F(k>1?[X,Y]:[h,v],-~n)),x]=c)(p)|r p => a => ( // given the starting position p[] and the matrix a[] r = // r = best result, initialized to a non-numeric value F = ( // F = recursive function taking: [x, y], // (x, y) = current position n, // n = number of shots, initially undefined R = a[y], // R = current row in the matrix c = R[x] // c = value of the current cell ) => // R[ // this will update R[x] once the inner code is executed c == (R[x] = 4) | // set the current cell to 4 (water); abort if it was n >= r || // already set to 4 or n is greater than or equal to r [-1, 0, 1, 2].map(d => // otherwise, for each direction d: (g = _ => ( // g = recursive function performing the shot by k = a[ // saving a backup (h, v) of (X, Y) v = Y, Y += d % 2][ // and updating (X, Y) until we reach a cell h = X, X += ~-d % 2]) // whose value k is not ice (k != 0) || g() // )(X = x, Y = y) // initial call to g() with (X, Y) = (x, y) > 3 ? // if k = 4 (water -> fail): 0 // abort immediately : // else: k > 2 ? // if k = 3 (hole -> success): r = -~n // set r to n + 1 : // else: F( // do a recursive call to F(): k > 1 ? // if k = 2 (sand): [X, Y] // start the next shots from the last cell : // else (wall): [h, v], // start from the last ice cell -~n // increment the number of shots ) // end of recursive call ), x // end of map(); x = actual index used to access R[] ] = c // restore the value of the current cell to c )(p) | r // initial call to F() at the starting position; return r #JavaScript (ES6), 174 bytes
p=>a=>(r=F=([x,y],n,R=a[y],c=R[x])=>R[c&(R[x]=4)|n>=r||[-1,0,1,2].map(d=>(g=_=>(k=a[v=Y,Y+=d%2][h=X,X+=~-d%2])||g())(X=x,Y=y)>3?0:k>2?r=-~n:F(k>1?[X,Y]:[h,v],-~n)),x]=c)(p)|r p => a => ( // given the starting position p[] and the matrix a[] r = // r = best result, initialized to a non-numeric value F = ( // F = recursive function taking: [x, y], // (x, y) = current position n, // n = number of shots, initially undefined R = a[y], // R = current row in the matrix c = R[x] // c = value of the current cell ) => // R[ // this will update R[x] once the inner code is executed c & (R[x] = 4) | // set the current cell to 4 (water); abort if it was n >= r || // already set to 4 or n is greater than or equal to r [-1, 0, 1, 2].map(d => // otherwise, for each direction d: (g = _ => ( // g = recursive function performing the shot by k = a[ // saving a backup (h, v) of (X, Y) v = Y, Y += d % 2][ // and updating (X, Y) until we reach a cell h = X, X += ~-d % 2]) // whose value k is not 0 (ice) || g() // )(X = x, Y = y) // initial call to g() with (X, Y) = (x, y) > 3 ? // if k = 4 (water -> fail): 0 // abort immediately : // else: k > 2 ? // if k = 3 (hole -> success): r = -~n // set r to n + 1 : // else: F( // do a recursive call to F(): k > 1 ? // if k = 2 (sand): [X, Y] // start the next shots from the last cell : // else (wall): [h, v], // start from the last ice cell -~n // increment the number of shots ) // end of recursive call ), x // end of map(); x = actual index used to access R[] ] = c // restore the value of the current cell to c )(p) | r // initial call to F() at the starting position; return r #JavaScript (ES6), 176175 bytes
p=>a=>(r=F=([x,y],n,R=a[y],c=R[x])=>R[c<=>R[c==(R[x]=4)&&[|n>=r||[-1,0,1,2].map(d=>(g=_=>(k=a[v=Y,Y+=d%2][h=X,X+=~-d%2])||g())(X=x,Y=y)>3?0:k>2?r=n>=r?r:r=-~n:F(k>1?[X,Y]:[h,v],-~n)),x]=c)(p)|r p => a => ( // given the starting position p[] and the matrix a[] r = // r = best result, initialized to a non-numeric value F = ( // F = recursive function taking: [x, y], // (x, y) = current position n, // n = number of shots, initially undefined R = a[y], // R = current row in the matrix c = R[x] // c = value of the current cell ) => // R[ // this will update R[x] once the inner code is executed c <== (R[x] = 4) &&| // set the current cell to water;4 (water); abort if it was not n >= r || // already set to 4 or n is greater than or equal to r [-1, 0, 1, 2].map(d => // set to waterotherwise, then for each direction d: (g = _ => ( // g = recursive function performing the shot by k = a[ // saving a backup (h, v) of (X, Y) v = Y, Y += d % 2][ // and updating (X, Y) until we reach a cell h = X, X += ~-d % 2]) // whose value k is not ice (k != 0) || g() // )(X = x, Y = y) // initial call to g() with (X, Y) = (x, y) > 3 ? // if k = 4 (water -> fail): 0 // abort immediately : // else: k > 2 ? // if k = 3 (hole -> success): r = n-~n >= r ? r : -~n // // if n is less than r, then set r to n+1n + 1 : // else: F( // do a recursive call to F(): k > 1 ? // if k = 2 (sand): [X, Y] // start the next shots from the last cell : // else (wall): [h, v], // start from the last ice cell -~n // increment the number of shots ) // end of recursive call ), x // end of map(); x = actual index used to access R[] ] = c // restore the value of the current cell to c )(p) | r // initial call to F() at the starting position; return r #JavaScript (ES6), 176 bytes
p=>a=>(r=F=([x,y],n,R=a[y],c=R[x])=>R[c<(R[x]=4)&&[-1,0,1,2].map(d=>(g=_=>(k=a[v=Y,Y+=d%2][h=X,X+=~-d%2])||g())(X=x,Y=y)>3?0:k>2?r=n>=r?r:-~n:F(k>1?[X,Y]:[h,v],-~n)),x]=c)(p)|r p => a => ( // given the starting position p[] and the matrix a[] r = // r = best result, initialized to a non-numeric value F = ( // F = recursive function taking: [x, y], // (x, y) = current position n, // n = number of shots R = a[y], // R = current row in the matrix c = R[x] // c = value of the current cell ) => // R[ // this will update R[x] once the inner code is executed c < (R[x] = 4) && // set the current cell to water; if it was not already [-1, 0, 1, 2].map(d => // set to water, then for each direction d: (g = _ => ( // g = recursive function performing the shot by k = a[ // saving a backup (h, v) of (X, Y) v = Y, Y += d % 2][ // and updating (X, Y) until we reach a cell h = X, X += ~-d % 2]) // whose value k is not ice (k != 0) || g() // )(X = x, Y = y) // initial call to g() with (X, Y) = (x, y) > 3 ? // if k = 4 (water): 0 // abort immediately : // else: k > 2 ? // if k = 3 (hole): r = n >= r ? r : -~n // if n is less than r, then set r to n+1 : // else: F( // do a recursive call to F(): k > 1 ? // if k = 2 (sand): [X, Y] // start the next shots from the last cell : // else (wall): [h, v], // start from the last ice cell -~n // increment the number of shots ) // end of recursive call ), x // end of map(); x = actual index used to access R[] ] = c // restore the value of the current cell to c )(p) | r // initial call to F() at the starting position; return r #JavaScript (ES6), 175 bytes
p=>a=>(r=F=([x,y],n,R=a[y],c=R[x])=>R[c==(R[x]=4)|n>=r||[-1,0,1,2].map(d=>(g=_=>(k=a[v=Y,Y+=d%2][h=X,X+=~-d%2])||g())(X=x,Y=y)>3?0:k>2?r=-~n:F(k>1?[X,Y]:[h,v],-~n)),x]=c)(p)|r p => a => ( // given the starting position p[] and the matrix a[] r = // r = best result, initialized to a non-numeric value F = ( // F = recursive function taking: [x, y], // (x, y) = current position n, // n = number of shots, initially undefined R = a[y], // R = current row in the matrix c = R[x] // c = value of the current cell ) => // R[ // this will update R[x] once the inner code is executed c == (R[x] = 4) | // set the current cell to 4 (water); abort if it was n >= r || // already set to 4 or n is greater than or equal to r [-1, 0, 1, 2].map(d => // otherwise, for each direction d: (g = _ => ( // g = recursive function performing the shot by k = a[ // saving a backup (h, v) of (X, Y) v = Y, Y += d % 2][ // and updating (X, Y) until we reach a cell h = X, X += ~-d % 2]) // whose value k is not ice (k != 0) || g() // )(X = x, Y = y) // initial call to g() with (X, Y) = (x, y) > 3 ? // if k = 4 (water -> fail): 0 // abort immediately : // else: k > 2 ? // if k = 3 (hole -> success): r = -~n // set r to n + 1 : // else: F( // do a recursive call to F(): k > 1 ? // if k = 2 (sand): [X, Y] // start the next shots from the last cell : // else (wall): [h, v], // start from the last ice cell -~n // increment the number of shots ) // end of recursive call ), x // end of map(); x = actual index used to access R[] ] = c // restore the value of the current cell to c )(p) | r // initial call to F() at the starting position; return r