Skip to main content
added 15 characters in body
Source Link
Justin Mariner
  • 5k
  • 1
  • 14
  • 18
f=a=>a.length>eval("for(i=0;a[i]>-1;)a[i]==a[++i]&&a.splice(--i,2,a[i]*2);i")?1+f(a):0 ;[ [], [1], [1,1], [2,1,1], [4,2,1,1], [2,2,2,1,1], [0,0,0,0], [4,0,0,0,4], [4,0,0,0,0,4], [] ].forEach(x=>console.log( JSON.stringify(x) + " -> " + f(x) ))
.as-console-wrapper{max-height:100%!important}
f=a=>a.length>eval("for(i=0;a[i]>-1;)a[i]==a[++i]&&a.splice(--i,2,a[i]*2);i")?1+f(a):0 ;[ [], [1], [1,1], [2,1,1], [4,2,1,1], [2,2,2,1,1], [0,0,0,0], [4,0,0,0,4] ].forEach(x=>console.log( JSON.stringify(x) + " -> " + f(x) ))
.as-console-wrapper{max-height:100%!important}
f=a=>a.length>eval("for(i=0;a[i]>-1;)a[i]==a[++i]&&a.splice(--i,2,a[i]*2);i")?1+f(a):0 ;[ [1], [1,1], [2,1,1], [4,2,1,1], [2,2,2,1,1], [0,0,0,0], [4,0,0,0,4], [4,0,0,0,0,4], [] ].forEach(x=>console.log( JSON.stringify(x) + " -> " + f(x) ))
.as-console-wrapper{max-height:100%!important}
added 822 characters in body
Source Link
Justin Mariner
  • 5k
  • 1
  • 14
  • 18
f=a=>a.length>eval("for(i=0;a[i]>-1;)a[i]==a[++i]&&a.splice(--i,2,a[i]*2);i")?1+f(a):0 

Ungolfed and Explained

f=a=> // function taking array a a.length > eval(" // if a.length > the result of the following... for(i=0; a[i]>-1;) // loop from 0 until the current value is undefined (which is not > -1) a[i] == a[++i] && // if the current value equals the next one... a.splice(--i, // splice the array at the first index of the pair... 2, // by replacing 2 items... a[i]*2); // with the current item * 2 // this also decrements the counter, which means the current value is now the next i") // return the counter, which is new a.length ? 1+f(a) // if that was true, the array was crushed. add 1 and recur with the new array : 0 // otherwise just return 0 
f=a=>a.length>eval("for(i=0;a[i]>-1;)a[i]==a[++i]&&a.splice(--i,2,a[i]*2);i")?1+f(a):0 
f=a=>a.length>eval("for(i=0;a[i]>-1;)a[i]==a[++i]&&a.splice(--i,2,a[i]*2);i")?1+f(a):0 

Ungolfed and Explained

f=a=> // function taking array a a.length > eval(" // if a.length > the result of the following... for(i=0; a[i]>-1;) // loop from 0 until the current value is undefined (which is not > -1) a[i] == a[++i] && // if the current value equals the next one... a.splice(--i, // splice the array at the first index of the pair... 2, // by replacing 2 items... a[i]*2); // with the current item * 2 // this also decrements the counter, which means the current value is now the next i") // return the counter, which is new a.length ? 1+f(a) // if that was true, the array was crushed. add 1 and recur with the new array : 0 // otherwise just return 0 
Source Link
Justin Mariner
  • 5k
  • 1
  • 14
  • 18

JavaScript (ES6), 86 bytes

f=a=>a.length>eval("for(i=0;a[i]>-1;)a[i]==a[++i]&&a.splice(--i,2,a[i]*2);i")?1+f(a):0 

Tests

f=a=>a.length>eval("for(i=0;a[i]>-1;)a[i]==a[++i]&&a.splice(--i,2,a[i]*2);i")?1+f(a):0 ;[ [], [1], [1,1], [2,1,1], [4,2,1,1], [2,2,2,1,1], [0,0,0,0], [4,0,0,0,4] ].forEach(x=>console.log( JSON.stringify(x) + " -> " + f(x) ))
.as-console-wrapper{max-height:100%!important}