I decided to go with javascript and use generators so I could easily suspend the amplifier program between inputs
constINSTRUCTIONS={ADD:1,MULT:2,INPUT:3,OUTPUT:4,JUMP_IF_TRUE:5,JUMP_IF_FALSE:6,LESS_THAN:7,EQUALS:8,HALT:99}functionrunProgram(instructions){instructions=instructions.slice()returnfunction*amplifier(){letlastOutput=nullfor(leti=0;i<instructions.length;++i){constinstruction=instructions[i]constparsed=String(instruction).padStart(5,'0').split('')constvalueMode=(value,mode='0')=>mode==='0'?instructions[value]:valueconstopCode=Number(parsed.slice(3).join(''))constmodes=parsed.slice(0,3)switch(opCode){caseINSTRUCTIONS.ADD:{constx=valueMode(instructions[++i],modes[2])consty=valueMode(instructions[++i],modes[1])instructions[instructions[++i]]=x+ybreak}caseINSTRUCTIONS.MULT:{constx=valueMode(instructions[++i],modes[2])consty=valueMode(instructions[++i],modes[1])instructions[instructions[++i]]=x*ybreak}caseINSTRUCTIONS.INPUT:{instructions[instructions[++i]]=yield{type:'INPUT'}break}caseINSTRUCTIONS.OUTPUT:{lastOutput=valueMode(instructions[++i],modes[2])yield{type:'OUTPUT',value:lastOutput}break}caseINSTRUCTIONS.JUMP_IF_TRUE:{constcompare=valueMode(instructions[++i],modes[2])constjumpTo=valueMode(instructions[++i],modes[1])-1if(compare!=0){i=jumpTo}break}caseINSTRUCTIONS.JUMP_IF_FALSE:{constcompare=valueMode(instructions[++i],modes[2])constjumpTo=valueMode(instructions[++i],modes[1])-1if(compare==0){i=jumpTo}break}caseINSTRUCTIONS.LESS_THAN:{constx=valueMode(instructions[++i],modes[2])consty=valueMode(instructions[++i],modes[1])instructions[instructions[++i]]=x<y?1:0break}caseINSTRUCTIONS.EQUALS:{constx=valueMode(instructions[++i],modes[2])consty=valueMode(instructions[++i],modes[1])instructions[instructions[++i]]=x===y?1:0break}caseINSTRUCTIONS.HALT:returnlastOutput}}}}constpermutations=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}functionpart1(instructions){constallInputs=permutations([0,1,2,3,4])constresults=allInputs.map(inputs=>{constamplifiers=inputs.map((input,i)=>{constamplifier=runProgram(instructions)()amplifier.next()// set phaseconststate=amplifier.next(input)constname=String.fromCharCode(i+65)return{amplifier,state,name}})letpower=0for(constampofamplifiers){const{amplifier}=ampconstout=amplifier.next(power)power=out.value.value}returnpower})returnresults.reduce((p,v)=>Math.max(p,v),0)}functionpart2(instructions){constallInputs=permutations([9,8,7,6,5])constresults=allInputs.map(inputs=>{constamplifiers=inputs.map((input,i)=>{constamplifier=runProgram(instructions)()amplifier.next()// set phaseconststate=amplifier.next(input)constname=String.fromCharCode(i+65)return{amplifier,state,name}})letpower=0while(amplifiers[amplifiers.length-1].state.done===false){for(constampofamplifiers){const{amplifier}=ampconstout=amplifier.next(power)// continue to next inputamp.state=amplifier.next()power=out.done?out.value:out.value.value}}returnpower})returnresults.reduce((p,v)=>Math.max(p,v),0)}if(process.argv[1]===__filename){constinput=require('fs').readFileSync(0).toString()constinstructions=input.split(',').map(Number)constp1Max=part1(instructions)console.log(p1Max)constp2Max=part2(instructions)console.log(p2Max)}
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.
I decided to go with javascript and use generators so I could easily suspend the amplifier program between inputs