I did Part 1 using arrays and I attempted Part 2 using indexOf to locate the input inside the array.
I was getting memory errors, so I wrote an algorithm that would always attempt to find the input as the last N elements of the scorecard, backwards. With a few optimizations, it runs in 5 seconds!
14a.js
constgetScoreBoard=input=>{constscoreboard=[3,7];letelf1=0;letelf2=1;while(scoreboard.length<input+10){constscoreElf1=scoreboard[elf1];constscoreElf2=scoreboard[elf2];constnewRecipes=(scoreElf1+scoreElf2).toString().split('').map(i=>+i);scoreboard.push(...newRecipes);const{length}=scoreboard;elf1=(elf1+scoreElf1+1)%length;elf2=(elf2+scoreElf2+1)%length;}returnscoreboard;};(()=>{constinput=880751;constscoreboard=getScoreBoard(input);constscores=scoreboard.slice(input,input+10).join('');console.log(`The scores of the ten recipes immediately after the given number of recipes is ${scores}`);})();
14b.js
constnumberOfRecipes=input=>{constscoreboard=[3,7];letelf1=0;letelf2=1;input=input.split('').map(i=>+i);constn=input.length;letm=scoreboard.length;letnumber;while(!number){constscoreElf1=scoreboard[elf1];constscoreElf2=scoreboard[elf2];constnewRecipes=(scoreElf1+scoreElf2).toString().split('').map(i=>+i);scoreboard.push(...newRecipes);m+=newRecipes.length;const{length}=scoreboard;elf1=(elf1+scoreElf1+1)%length;elf2=(elf2+scoreElf2+1)%length;letfoundInput=false;for(lettries=0;!foundInput&&tries<newRecipes.length;tries++){lethasNotFoundInputYet=false;for(leti=1;!hasNotFoundInputYet&&i<=n;i++){if(scoreboard[m-i-tries]!==input[n-i]){hasNotFoundInputYet=true;}}foundInput=!hasNotFoundInputYet;}if(foundInput){number=m-n;}}returnnumber;};(()=>{constinput='880751';constnumber=numberOfRecipes(input);console.log(`The number of recipes which appear on the scoreboard to the left of input is ${number}`);})();
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript solution
I did Part 1 using arrays and I attempted Part 2 using indexOf to locate the input inside the array.
I was getting memory errors, so I wrote an algorithm that would always attempt to find the input as the last N elements of the scorecard, backwards. With a few optimizations, it runs in 5 seconds!
14a.js
14b.js