DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 13: Care Package

Collapse
 
maxart2501 profile image
Massimo Artizzu

This was much easier indeed!

Part One

The only thing I've given for granted is that the machine, on the first part, never overwrites a tile, but I was ready to track that down eventually. I'm reporting just the relevant parts in JavaScript:

const game = createProgramInstance(codes, 1);
let blocks = 0;
while (true) {
  const { value: x } = game.next();
  if (typeof x === 'undefined') {
    break;
  }
  game.next();
  const { value: draw } = game.next();
  if (draw === 2) {
    blocks++;
  }
}
console.log(blocks);

Part Two

I don't know if you noticed the hidden message in the text: "You do have crew quarters, but they won't fit in the machine." 😂
Anyway, after the initial confusion about "how to play this game?!", I realized this is just Arkanoid/Breakout! 😄 I just have to move the paddle left and right to reach the ball.
The only change I did to the main routing was to set a fixed input value instead of a stack of values (joystickPosition).

const game = createProgramInstance(codes, 1);
let score;
let ballX;
let paddleX;
while (true) {
  const { value: x } = game.next();
  if (typeof x === 'undefined') {
    break;
  }
  const { value: y } = game.next();
  const { value } = game.next();
  if (x === -1 && y === 0) {
    score = value;
  } else if (value === 3) {
    paddleX = x;
  } else if (value === 4) {
    ballX = x;
  }
  joystickPosition = Math.sign(ballX - paddleX);
}
console.log(score);

Get my input at my repo.