DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 11: Space Police

Collapse
 
maxart2501 profile image
Massimo Artizzu

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 BigInts since they are not necessary.

So the main running cycle became (JavaScript ahead!):

for (const instruction of getInstructions()) {
  if (instruction.opcode === 99) {
    return;
  }
  const output = execute(instruction);
  if (typeof output === 'number') {
    const newValue = yield output;
    if (typeof newValue !== 'undefined') {
      stack.push(newValue);
    }
  }
}

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 corrections
const paintedPanels = new Map();
const robot = createProgramInstance(codes, 0);
let direction = 0;
let row = 0;
let column = 0;
let currentPaint;
while (true) {
  const { value: paint } = robot.next(currentPaint);
  const coords = 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) {
    case 0: row--; break;
    case 1: column++; break;
    case 2: row++; break;
    case 3: column--; break;
  }
  currentPaint = paintedPanels.get(column + ',' + row) || 0;
}
console.log(paintedPanels.size);

Part Two

// As above, except it starts with 1 instead of 0:
const robot = createProgramInstance(codes, 1);

// This comes instead of the last console.log
const paintedPanelsCoords = [ ...paintedPanels.keys() ].map(coords => coords.split(','));
const paintedColumns = paintedPanelsCoords.map(([ column ]) => column);
const paintedRows = paintedPanelsCoords.map(([ , row ]) => row);
const minColumn = Math.min(...paintedColumns);
const maxColumn = Math.max(...paintedColumns);
const minRow = Math.min(...paintedRows);
const maxRow = Math.max(...paintedRows);

// Pretty printing the result
for (let row = minRow; row <= maxRow; row++) {
  const line = Array.from({ length: maxColumn - minColumn + 1 }, (_, index) => {
    return paintedPanels.get(minColumn + index + ',' + row) ? '#' : '.';
  }).join('');
  console.log(line);
}