My case was exactly the same as @jmgimeno
, I used the same analyzer from day 19 and I came down to the same conclusions as him:
for part 1, the solution is the first time the code reaches the check if register[5] is the same as register0
for part 2, I decided that I had to check for loops and the last solution before the 1st loop is the solution.
As usual, I'm gonna omit reader.js which is the same as the other solutions and jump to the point:
21a.js
const{readFile}=require('./reader');const{operations}=require('./16-common');const{readInput,analyzeProgram}=require('./19-common');construnProgram=(ipRegister,program)=>{letregisters=[0,0,0,0,0,0];constn=program.length;letip=registers[ipRegister];while(ip>=0&&ip<n){constinstruction=program[ip];constop=operations[instruction[0]];registers=op(instruction)(registers);ip=++registers[ipRegister];if(ip===28){returnregisters[5];}}returnregisters[5];};(async()=>{constlines=awaitreadFile('21-input.txt');const{ipRegister,program}=readInput(lines);//analyzeProgram(ipRegister, program);constsolution=runProgram(ipRegister,program);console.log(`The lowest non-negative integer value for register 0 that causes the program to halt after executing the fewest instructions is ${solution}.`);})();
21b.js
const{readFile}=require('./reader');const{operations}=require('./16-common');const{readInput,analyzeProgram}=require('./19-common');construnProgram=(ipRegister,program)=>{letregisters=[0,0,0,0,0,0];constn=program.length;letip=registers[ipRegister];constfrequency=newSet();letlastSolution;while(ip>=0&&ip<n){constinstruction=program[ip];constop=operations[instruction[0]];registers=op(instruction)(registers);ip=++registers[ipRegister];if(ip===28){if(frequency.has(registers[5])){returnlastSolution;}else{lastSolution=registers[5];frequency.add(lastSolution);}}}returnregisters[5];};(async()=>{constlines=awaitreadFile('21-input.txt');const{ipRegister,program}=readInput(lines);//analyzeProgram(ipRegister, program);constsolution=runProgram(ipRegister,program);console.log(`The lowest non-negative integer value for register 0 that causes the program to halt after executing the most instructions is ${solution}.`);})();
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
My case was exactly the same as @jmgimeno , I used the same analyzer from day 19 and I came down to the same conclusions as him:
As usual, I'm gonna omit reader.js which is the same as the other solutions and jump to the point:
21a.js
21b.js