JavaScript checking in! I think it'll be interesting, seeing how we re-use this in future challenges. It did take me reading the end of part 2 a number of times before I understood what I was looking for! :)
I also feel like there should be a better way to process the second part...I saw folks on the subreddit talking about just changing the noun value until it's as high as can be while still below the desired output, then changing the verb, but it seems maybe there's unforeseen issues with that? I don't know, everyone I've seen so far is just nesting for loops so I'm not that worried about it. :)
// copy of initial input, so we can reset properlyletinputCopy=[...input];// noun - input for address 1; verb - input for address 2letnoun=12;letverb=2;// for part 2 - desired output to match - output is final value at address 0 when program is doneconstdesiredOutput=19690720;// opcode 1 - get values at position 1&2 right after code, add together, store in position 3// opcode 2 - get values at position 1&2 right after code, multiply, store in position 3// opcode 99 - stop programfunctionintCode(op,a,b,c){letvalA=inputCopy[a];letvalB=inputCopy[b];if(op===1){inputCopy[c]=valA+valB;}elseif(op===2){inputCopy[c]=valA*valB;}}// run through memory input, following instructions until 99 is hitfunctionrunProgram(){for(leti=0;i<inputCopy.length;i+=4){if(inputCopy[i]===99){break;}intCode(inputCopy[i],inputCopy[i+1],inputCopy[i+2],inputCopy[i+3]);}}// for part 1 - insert noun & verb providedinputCopy[1]=noun;inputCopy[2]=verb;runProgram();console.log(`Part 1: position 0 value is ${inputCopy[0]}`);// part 2 - test different values for noun & verb, insert into memory, run program - when desired output is matched, log noun & verb used & end cyclefor(letn=0;n<100;n++){noun=n;letfound=false;for(letv=0;v<100;v++){verb=v;// reset copy to initial input, then replace noun & verbinputCopy=[...input];inputCopy[1]=noun;inputCopy[2]=verb;runProgram();letcurrentOutput=inputCopy[0];if(currentOutput===desiredOutput){found=true;break;}}if(found){break;}}console.log(`desired noun and verb are ${noun} & ${verb}; output value is ${inputCopy[0]}`);console.log(`Part 2 - computed value is ${100*noun+verb}`);
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 checking in! I think it'll be interesting, seeing how we re-use this in future challenges. It did take me reading the end of part 2 a number of times before I understood what I was looking for! :)
I also feel like there should be a better way to process the second part...I saw folks on the subreddit talking about just changing the noun value until it's as high as can be while still below the desired output, then changing the verb, but it seems maybe there's unforeseen issues with that? I don't know, everyone I've seen so far is just nesting for loops so I'm not that worried about it. :)