Quantcast
Channel: User Yay295 - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 12

Answer by Yay295 for Array Escape - get out of there

$
0
0

JavaScript, 121 132 bytes

p=n=>t=i=>n%i&&n>i?t(i+1):(0<n&&n<=i?1:0),c=-1,a=>r=s=>(++c,0<=s&&s<a.length?(p(s)(2)?r(s-a[s]):0||([a[s],s]=[0,a[s]])[1]?r(s):0):c)

f=(p=n=>t=i=>n%i&&n>i?t(i+1):(0<n&&n<=i?1:0),c=-1,a=>r=s=>(++c,0<=s&&s<a.length?(p(s)(2)?r(s-a[s]):0||([a[s],s]=[0,a[s]])[1]?r(s):0):c));let test_data = [[[1,4,5,6,8,10,14,15,2,2,4,5,7],4],                 [[2,5,6,8,1,2,3],3],                 [[2,0,2],2],                 [[14,1,2,5,1,3,51,5,12,3,4,41,15,4,12,243,51,2,14,51,12,11],5]];for (test of test_data) {    c = -1;    console.log(f(test[0])(test[1]));}

edit 1: oops, missed the bit about returning number of steps. fix coming soonish.

edit 2: fixed


Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>