I wrote lots of bugs and ended up with ugly code. Sorry for this mess:
functionprocess(){letinput=5;//input = 1; /* for part 1 */constpretext=document.getElementsByTagName('pre')[0].innerHTML;letinstructions=pretext.split(',');letinstructionLength=0;for(leti=0;i<instructions.length;i+=instructionLength){letinst=instructions[i].padStart(5,'0');if(inst==='00099'){break;}letopcode=inst.slice(-2);letparameterModes=inst.substr(0,3).split('').reverse();if(['01','02','07','08'].indexOf(opcode)>-1){instructionLength=4;letvalues=[0,0];for(letpi=0;pi<values.length;pi++){values[pi]=parseInt(instructions[i+pi+1],10);if(parameterModes[pi]==='0'){values[pi]=parseInt(instructions[values[pi]],10);}}letanswerPos=parseInt(instructions[i+3],10);if(opcode==='01'){instructions[answerPos]=(values[0]+values[1]).toString();}elseif(opcode==='02'){instructions[answerPos]=(values[0]*values[1]).toString();}elseif(opcode==='07'){instructions[answerPos]=values[0]<values[1]?'1':'0';}elseif(opcode==='08'){instructions[answerPos]=values[0]===values[1]?'1':'0';}}elseif(opcode==='05'||opcode=='06'){instructionLength=3;letparameterModes=inst.substr(0,3).split('').reverse();letvalue=parseInt(instructions[i+1]);if(parameterModes[0]==0){value=parseInt(instructions[value],10);}letpoint=parseInt(instructions[i+2]);if(parameterModes[1]==0){point=parseInt(instructions[point],10);}if(opcode==='05'&&value!==0){i=point;instructionLength=0;}if(opcode==='06'&&value===0){i=point;instructionLength=0;}}else{instructionLength=2;letposition=parseInt(instructions[i+1],10);if(opcode==='03'){instructions[position]=input.toString();}elseif(opcode==='04'){console.log(instructions[position]);}}}returninstructions[0];}
I like how this can still be used to solve day 2 😁
I wrote lots of bugs and ended up with ugly code. Sorry for this mess:
I like how this can still be used to solve day 2 😁
Ooh, now I have to load day 2’s code. 🤩