But fortunately at this point I'm pretty confident of my routines, so I just copy+pasted most of it. Tweaked a bit since after each output it could or could not receive an input (the scanned color). Also ditched BigInts since they are not necessary.
So the main running cycle became (JavaScript ahead!):
For the rest, I'm keeping track of the coordinates, the direction (an integer between 0 and 3) and the last read panel color. Everything worked at the first try!
Part One
// `createProgramInstance` as defined on day 9, with the above correctionsconstpaintedPanels=newMap();constrobot=createProgramInstance(codes,0);letdirection=0;letrow=0;letcolumn=0;letcurrentPaint;while(true){const{value:paint}=robot.next(currentPaint);constcoords=column+','+row;if(paintedPanels.get(coords)===paint){break;// It breaks out after 9515 cycles for me...}paintedPanels.set(coords,paint);const{value:turn}=robot.next();direction=(direction+(turn===0?3:1))%4;switch(direction){case0:row--;break;case1:column++;break;case2:row++;break;case3:column--;break;}currentPaint=paintedPanels.get(column+','+row)||0;}console.log(paintedPanels.size);
Part Two
// As above, except it starts with 1 instead of 0:constrobot=createProgramInstance(codes,1);// This comes instead of the last console.logconstpaintedPanelsCoords=[...paintedPanels.keys()].map(coords=>coords.split(','));constpaintedColumns=paintedPanelsCoords.map(([column])=>column);constpaintedRows=paintedPanelsCoords.map(([,row])=>row);constminColumn=Math.min(...paintedColumns);constmaxColumn=Math.max(...paintedColumns);constminRow=Math.min(...paintedRows);constmaxRow=Math.max(...paintedRows);// Pretty printing the resultfor(letrow=minRow;row<=maxRow;row++){constline=Array.from({length:maxColumn-minColumn+1},(_,index)=>{returnpaintedPanels.get(minColumn+index+','+row)?'#':'.';}).join('');console.log(line);}
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.
I knew we were going to use IntCodes again! 🧐
But fortunately at this point I'm pretty confident of my routines, so I just copy+pasted most of it. Tweaked a bit since after each output it could or could not receive an input (the scanned color). Also ditched
BigInt
s since they are not necessary.So the main running cycle became (JavaScript ahead!):
For the rest, I'm keeping track of the coordinates, the direction (an integer between 0 and 3) and the last read panel color. Everything worked at the first try!
Part One
Part Two