Horray, finally got part 2 solved!! happy dance Took FOREVER but I finally got it working. This will be the part 2 solution in JavaScript (part 1 was very similar, just slightly more simplified and didn't involve the objects I created for each amp).
// Shout out to MikeTheReader & Morganamilo for the help with part 2 of this one!// Memory - initial puzzle input, a list of integersconstinput=[3,8,1001,8,10.....3,9,1002,9,99];// copy of initial input, so we can reset properlyletinputCopy=[...input];// opcode 1 - get values at position 1&2 right after code, add together, store in position 3functionopcode1(a,b,c,p,dir){letvalA=ptest(p[0],a,dir);letvalB=ptest(p[1],b,dir);dir[c]=valA+valB;// console.log(`op1: ${valA} + ${valB} = ${valA + valB}`)}// opcode 2 - get values at position 1&2 right after code, multiply, store in position 3functionopcode2(a,b,c,p,dir){letvalA=ptest(p[0],a,dir);letvalB=ptest(p[1],b,dir);dir[c]=valA*valB;// console.log(`op2: ${valA} * ${valB} = ${valA * valB}`)}// opcode 3 - takes an input and stores in position 1functionopcode3(iv,s,dir){dir[s]=iv;// console.log(`op3: putting ${iv} into spot ${s}`)}// opcode 4 - outputs value at position 1functionopcode4(s,p,dir){letval=ptest(p[0],s,dir);// console.log(`op4: outputting ${val}`)returnval;}// opcode 5 - if position 1 != 0, changes i to position 2; otherwise, does nothingfunctionopcode5(a,b,inp,p,dir){letvalA=ptest(p[0],a,dir);letvalB=ptest(p[1],b,dir);if(valA!==0){inp=valB;}// console.log(`op5: inst. pointer is now ${inp}`);returninp;}// opcode 6 - if position 1 == 0, changes i to position 2; otherwise, does nothingfunctionopcode6(a,b,inp,p,dir){letvalA=ptest(p[0],a,dir);letvalB=ptest(p[1],b,dir);if(valA===0){inp=valB;}// console.log(`op6: inst. pointer is now ${inp}`);returninp;}// opcode 7 - if position 1 < position 2, position 3 is set to 1; otherwise, it's set to 0functionopcode7(a,b,c,p,dir){letvalA=ptest(p[0],a,dir);letvalB=ptest(p[1],b,dir);if(valA<valB){dir[c]=1;}else{dir[c]=0;}// console.log(`op7: comparing if ${valA} is < ${valB}`);}// opcode 8 - if position 1 == position 2, position 3 is set to 1; otherwise, it's set to 0functionopcode8(a,b,c,p,dir){letvalA=ptest(p[0],a,dir);letvalB=ptest(p[1],b,dir);if(valA==valB){dir[c]=1;}else{dir[c]=0;}// console.log(`op8: comparing if ${valA} equals ${valB}`);}// allows parameter modes - checks for 0 or 1, decides if returning actual number called or position of number in inputfunctionptest(param,checkval,dir){if(param==0||!param){returndir[checkval];}elseif(param==1){returncheckval;}}// opcode 99 - stop program// run through memory input, following instructions until 99 is hitfunctionrunProgram(amp){for(leti=amp.pointer;i<inputCopy.length;i++){if(amp.directions[i]===99){if(amp.haltCalled){returnamp.output;}amp.haltCalled=true;haltsCalled++;amp.pointer=i;returnamp.output;}letinstruct=amp.directions[i].toString();letopval=parseInt(instruct.slice(-2),10);letparams=instruct.slice(0,-2).split('').reverse();letione=amp.directions[i+1];letitwo=amp.directions[i+2];letithree=amp.directions[i+3];switch(opval){case01:opcode1(ione,itwo,ithree,params,amp.directions);i+=3;break;case02:opcode2(ione,itwo,ithree,params,amp.directions);i+=3;break;case03:if(amp.requestedInputs===0){opcode3(amp.inputPhase,ione,amp.directions);i++;amp.requestedInputs++;break;}elseif(amp.requestedInputs===1){opcode3(amp.inputInit,ione,amp.directions);i++;amp.requestedInputs++;break;}else{opcode3(amp.inputSig,ione,amp.directions);i++;break;}case04:letres=opcode4(ione,params,amp.directions);amp.output=res;i++;amp.pointer=i+1;returnamp.output;break;case05:letcheckt=opcode5(ione,itwo,i,params,amp.directions);if(i!=checkt){i=checkt-1;}else{i+=2;}break;case06:letcheckf=opcode6(ione,itwo,i,params,amp.directions);if(i!=checkf){i=checkf-1;}else{i+=2;}break;case07:opcode7(ione,itwo,ithree,params,amp.directions);i+=3;break;case08:opcode8(ione,itwo,ithree,params,amp.directions);i+=3;break;}}}letmaxSignal=0;// Didn't feel like creating this, so found a method here: https://stackoverflow.com/a/20871714constpermutator=(inputArr)=>{letresult=[];constpermute=(arr,m=[])=>{if(arr.length===0){result.push(m)}else{for(leti=0;i<arr.length;i++){letcurr=arr.slice();letnext=curr.splice(i,1);permute(curr.slice(),m.concat(next))}}}permute(inputArr);returnresult;}letphaseSettingOptions=[0,1,2,3,4];letphaseSettingOptions2=[5,6,7,8,9];letphaseSettings=permutator(phaseSettingOptions);letphaseSettings2=permutator(phaseSettingOptions2);// phase 2 settingsletamps=[{'name':'a','pointer':0,'inputPhase':0,'inputInit':0,'inputSig':0,'requestedInputs':0,'output':0,'haltCalled':false,'directions':[...input]},{'name':'b','pointer':0,'inputPhase':0,'inputInit':0,'inputSig':0,'requestedInputs':0,'output':0,'haltCalled':false,'directions':[...input]},{'name':'c','pointer':0,'inputPhase':0,'inputInit':0,'inputSig':0,'requestedInputs':0,'output':0,'haltCalled':false,'directions':[...input]},{'name':'d','pointer':0,'inputPhase':0,'inputInit':0,'inputSig':0,'requestedInputs':0,'output':0,'haltCalled':false,'directions':[...input]},{'name':'e','pointer':0,'inputPhase':0,'inputInit':0,'inputSig':0,'requestedInputs':0,'output':0,'haltCalled':false,'directions':[...input]},];lethaltsCalled=0;functionrunAmp(phase,init,sig,amp){amp.inputPhase=phase;amp.inputInit=init;amp.inputSig=sig;letnextInput=runProgram(amp);returnnextInput;}functionrunCycle(phase){for(letj=0;j<amps.length;j++){letprevious=j-1;if(previous<0){previous=4;}if(amps[j].pointer===0){amps[j].inputInit=runAmp(phase[j],amps[previous].output,0,amps[j]);}else{amps[j].inputSig=runAmp(phase[j],amps[j].inputInit,amps[previous].output,amps[j]);}}}functionresetValues(){for(leta=0;a<amps.length;a++){letthisAmp=amps[a];thisAmp.pointer=0;thisAmp.inputPhase=0;thisAmp.inputInit=0;thisAmp.inputSig=0;thisAmp.requestedInputs=0;thisAmp.output=0;thisAmp.haltCalled=false;thisAmp.directions=[...input];}};for(leti=0;i<phaseSettings2.length;i++){letphaseOrder=phaseSettings2[i];haltsCalled=0;resetValues();do(runCycle(phaseOrder));while(haltsCalled!==5);if(maxSignal===0){maxSignal=amps[4].output;}elseif(amps[4].output>maxSignal){maxSignal=amps[4].output;console.log(`best phase: ${phaseOrder}`);}}console.log(`Max signal: ${maxSignal}`);
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.
Horray, finally got part 2 solved!! happy dance Took FOREVER but I finally got it working. This will be the part 2 solution in JavaScript (part 1 was very similar, just slightly more simplified and didn't involve the objects I created for each amp).