Anyway... JavaScript. It has this nice generator thing that I basically never used professionally and very rarely by myself, and here I am using them twice here. But you know, they're quite good for stateful machines. (Which is also why I rarely use them - like many others, I prefer stateless approaches.)
This is the solution for the second part, as the first can run the first (except for the phase settings, where I just add 5 to the permutation's elements - see at the end).
Many parts are the same of day 5 of course. Sorry for the repetition.
constparameterCount={1:3,2:3,3:1,4:1,5:2,6:2,7:3,8:3,99:0};constcodes=input.split(',').map(Number);// As amplifiers are now stateful machines, we'll sort out to generators again.// A copy of the memory and the input stack is given.function*createProgramInstance(localCodes,...stack){letinstructionPointer=0;function*getInstructions(){while(instructionPointer<localCodes.length){constinstruction=localCodes[instructionPointer];constopcode=instruction%100;constmodes=Array.from({length:3},(_,index)=>Math.floor(instruction/10**(index+2)%10));constparamCount=parameterCount[opcode];constparams=localCodes.slice(instructionPointer+1,instructionPointer+paramCount+1);instructionPointer+=paramCount+1;yield{opcode,modes,params};}}functiongetValue(index,mode){returnmode===0?localCodes[index]:index;}functionexecute({opcode,modes,params}){switch(opcode){case1:localCodes[params[2]]=getValue(params[0],modes[0])+getValue(params[1],modes[1]);break;case2:constresult=getValue(params[0],modes[0])*getValue(params[1],modes[1]);localCodes[params[2]]=result;break;case3:localCodes[params[0]]=stack.shift();break;case4:returngetValue(params[0],modes[0]);case5:if(getValue(params[0],modes[0])){instructionPointer=getValue(params[1],modes[1]);}break;case6:if(getValue(params[0],modes[0])===0){instructionPointer=getValue(params[1],modes[1]);}break;case7:localCodes[params[2]]=+(getValue(params[0],modes[0])<getValue(params[1],modes[1]));break;case8:localCodes[params[2]]=+(getValue(params[0],modes[0])===getValue(params[1],modes[1]));break;}}for(constinstructionofgetInstructions()){if(instruction.opcode===99){return;}constoutput=execute(instruction);if(typeofoutput==='number'){// In order to resume execution, we get a new input (passed with .next())stack.push(yieldoutput);}}}// Given a number, returns the corresponding permutation of 0-length.// Can surely be improved, but it's definitely not the bottleneck.functiongetPermutation(length,index){constitems=Array.from({length},(_,i)=>i);letfactorial=1;returnitems.reduce((permutation,num)=>{constitemIdx=Math.floor(index/factorial)%(num+1);factorial*=num+1;permutation.splice(itemIdx,0,num);returnpermutation;},[]);}functioncreateAmplifier(phaseSetting,initialInput){constampCodes=[...codes];returncreateProgramInstance(ampCodes,phaseSetting,initialInput);}constAMPS=5;// Basically, AMPS!letpermutationCount=1;for(leti=1;i<=AMPS;i++){permutationCount*=i;}functionrunPermutation(permutation){letpreviousOutput=0;constamplifiers=[];letcounter=0;while(true){constindex=counter%AMPS;if(amplifiers.length-1<index){amplifiers.push(createAmplifier(permutation[index],previousOutput));}constamplifier=amplifiers[index];const{value}=amplifier.next(previousOutput);if(typeofvalue==='number'){previousOutput=value;}else{returnpreviousOutput;}counter++;}}constresults=[];for(leti=0;i<permutationCount;i++){constpermutation=getPermutation(AMPS,i).map(num=>num+5);constresult=runPermutation(permutation);results.push(result);}console.log(Math.max(...results));
Yoooo and I thought day 5 was annoying 😫
Anyway... JavaScript. It has this nice generator thing that I basically never used professionally and very rarely by myself, and here I am using them twice here. But you know, they're quite good for stateful machines. (Which is also why I rarely use them - like many others, I prefer stateless approaches.)
This is the solution for the second part, as the first can run the first (except for the phase settings, where I just add 5 to the permutation's elements - see at the end).
Many parts are the same of day 5 of course. Sorry for the repetition.
See text, input and first part on my repo.