So, I actually reasonably enjoyed this day! :) It did take a good bit of deciphering exactly what I needed to make it do....the wording is so similar in the description of things! But once I got it, I knew what to do, which I honestly really like. lol :)
I did refactor a bit of what I did for day 2, so this currently contains all of my opcode options. Fingers crossed it will be easier to add onto when the next day comes to update it! (Though knowing AoC, chances of that might be slim lol)
I've got a decent bit of descriptive comments in here, to help make things easier for myself in the future. :) So hopefully that helps some folks out, too! Also, I really enjoy switch statements, and I like that they've worked for multiple days so far this year. lol
// Memory - initial puzzle input, a list of integersconstinput=[3,225,1,225,6,6,1100,99,226];// shortened input for brevity// copy of initial input, so we can reset properlyletinputCopy=[...input];// opcode 1 - get values at position 1&2 right after code, add together, store in position 3functionopcode1(a,b,c,p){letvalA=ptest(p[0],a);letvalB=ptest(p[1],b);inputCopy[c]=valA+valB;console.log(`op1: ${valA} + ${valB} = ${valA+valB}`)}// opcode 2 - get values at position 1&2 right after code, multiply, store in position 3functionopcode2(a,b,c,p){letvalA=ptest(p[0],a);letvalB=ptest(p[1],b);inputCopy[c]=valA*valB;console.log(`op2: ${valA} * ${valB} = ${valA*valB}`)}// opcode 3 - takes an input and stores in position 1functionopcode3(iv,s){inputCopy[s]=iv;console.log(`op3: putting ${iv} into spot ${s}`)}// opcode 4 - outputs value at position 1functionopcode4(s,p){letval=ptest(p[0],s);console.log(`op4: outputting ${val}`)returnval;}// opcode 5 - if position 1 != 0, changes i to position 2; otherwise, does nothingfunctionopcode5(a,b,inp,p){letvalA=ptest(p[0],a);letvalB=ptest(p[1],b);if(valA!==0){inp=valB;}console.log(`op5: inst. pointer is now ${inp}`);returninp;}// opcode 6 - if position 1 == 0, changes i to position 2; otherwise, does nothingfunctionopcode6(a,b,inp,p){letvalA=ptest(p[0],a);letvalB=ptest(p[1],b);if(valA===0){inp=valB;}console.log(`op6: inst. pointer is now ${inp}`);returninp;}// opcode 7 - if position 1 < position 2, position 3 is set to 1; otherwise, it's set to 0functionopcode7(a,b,c,p){letvalA=ptest(p[0],a);letvalB=ptest(p[1],b);if(valA<valB){inputCopy[c]=1;}else{inputCopy[c]=0;}console.log(`op7: comparing if ${valA} is < ${valB}`);}// opcode 8 - if position 1 == position 2, position 3 is set to 1; otherwise, it's set to 0functionopcode8(a,b,c,p){letvalA=ptest(p[0],a);letvalB=ptest(p[1],b);if(valA==valB){inputCopy[c]=1;}else{inputCopy[c]=0;}console.log(`op8: comparing if ${valA} equals ${valB}`);}// allows parameter modes - checks for 0 or 1, decides if returning actual number called or position of number in inputfunctionptest(param,checkval){if(param==0||!param){returninputCopy[checkval];}elseif(param==1){returncheckval;}}// opcode 99 - stop program// run through memory input, following instructions until 99 is hitfunctionrunProgram(){for(leti=0;i<inputCopy.length;i++){if(inputCopy[i]===99){break;}letinstruct=inputCopy[i].toString();letopval=parseInt(instruct.slice(-2),10);letparams=instruct.slice(0,-2).split('').reverse();letione=inputCopy[i+1];letitwo=inputCopy[i+2];letithree=inputCopy[i+3];switch(opval){case01:opcode1(ione,itwo,ithree,params);i+=3;break;case02:opcode2(ione,itwo,ithree,params);i+=3;break;case03:opcode3(inputval,ione);i++;break;case04:letres=opcode4(ione,params);console.log(res);i++;break;case05:letcheckt=opcode5(ione,itwo,i,params);if(i!=checkt){i=checkt-1;}else{i+=2;}break;case06:letcheckf=opcode6(ione,itwo,i,params);if(i!=checkf){i=checkf-1;}else{i+=2;}break;case07:opcode7(ione,itwo,ithree,params);i+=3;break;case08:opcode8(ione,itwo,ithree,params);i+=3;break;}}}// for part 1, inputval is 1; for part 2, it's 5letinputval=5;runProgram();
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.
So, I actually reasonably enjoyed this day! :) It did take a good bit of deciphering exactly what I needed to make it do....the wording is so similar in the description of things! But once I got it, I knew what to do, which I honestly really like. lol :)
I did refactor a bit of what I did for day 2, so this currently contains all of my opcode options. Fingers crossed it will be easier to add onto when the next day comes to update it! (Though knowing AoC, chances of that might be slim lol)
I've got a decent bit of descriptive comments in here, to help make things easier for myself in the future. :) So hopefully that helps some folks out, too! Also, I really enjoy switch statements, and I like that they've worked for multiple days so far this year. lol