I have a generic solution for Part 1 and a customized solution to my input for Part 2.
For Part 2, because it was taking forever, I decided to print the output and let it run for a few minutes and try to understand how the registers vary and why.
Then I followed the suggestions from @jmgimeno
and analyzed my program to understand what should happen and what does the loop try to accomplish, and after some deep thoughts I concluded the register 0 is the sum of the dividers of the number in register 2! So I did that algorithm myself, manually.
I'm gonna omit reader.js which is the same as the other solutions and jump to the point:
const{readFile}=require('./reader');const{readInput,runProgram}=require('./19-common');(async()=>{constlines=awaitreadFile('19-input.txt');const{ipRegister,program}=readInput(lines);constresult=runProgram(ipRegister,program,[0,0,0,0,0,0]);console.log(`The state of the registers after executing the test program is ${result}`);})();
19b.js
const{readFile}=require('./reader');const{readInput,runProgram}=require('./19-common');constanalyzeProgram=(ipRegister,program)=>{for(leti=0;i<program.length;i++){const[op,a,b,c]=program[i];letanalysis;consttarget=c===ipRegister?'jumps ip to ':`[${c}] = `;if(op==='addi'){//result[c] = registers[a] + b; analysis=target+`[${a}] + ${b}`;}elseif(op==='addr'){//result[c] = registers[a] + registers[b]; analysis=target+`[${a}] + [${b}]`;}elseif(op==='seti'){//result[c] = a; analysis=target+`${a}`;}elseif(op==='setr'){//result[c] = registers[a]; analysis=target+`[${a}]`;}elseif(op==='muli'){//result[c] = registers[a] * b; analysis=target+`[${a}] * ${b}`;}elseif(op==='mulr'){//result[c] = registers[a] * registers[b]; analysis=target+`[${a}] * [${b}]`;}elseif(op==='eqrr'){//result[c] = registers[a] === registers[b] ? 1 : 0;analysis=target+`1 if [${a}] === [${b}] or 0 otherwise`;}elseif(op==='gtrr'){//result[c] = registers[a] > registers[b] ? 1 : 0; analysis=target+`1 if [${a}] > [${b}] or 0 otherwise`;}console.log(`${i}: ${analysis}`);}};construnOptimizedProgram=()=>{constnumber=10551298;letsumDivisors=0;for(leti=1;i<=number;i++){if(number%i===0){sumDivisors+=i;}}returnsumDivisors;}(async()=>{constlines=awaitreadFile('19-input.txt');//const { ipRegister, program } = readInput(lines);//analyzeProgram(ipRegister, program);//const result = runProgram(ipRegister, program, [1, 0, 0, 0, 0, 0]);constresult=runOptimizedProgram();console.log(`The state of the registers after executing the test program is ${result}`);})();
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 have a generic solution for Part 1 and a customized solution to my input for Part 2.
For Part 2, because it was taking forever, I decided to print the output and let it run for a few minutes and try to understand how the registers vary and why.
Then I followed the suggestions from @jmgimeno and analyzed my program to understand what should happen and what does the loop try to accomplish, and after some deep thoughts I concluded the register 0 is the sum of the dividers of the number in register 2! So I did that algorithm myself, manually.
I'm gonna omit reader.js which is the same as the other solutions and jump to the point:
19-common.js
19a.js
19b.js