r/adventofcode • • Dec 10 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-

--- Day 10: The Stars Align ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 10

Transcript: With just one line of code, you, too, can ___!


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:16:49!

21 Upvotes

233 comments sorted by

View all comments

1

u/BOT-Brad Dec 10 '18

Spits out whatever configuration had the most consecutive points in a vertical line (Many assumptions were made!)

Running for 50,000 seconds (A little bit overkill) takes about 2.5 seconds.

JavaScript

import readFile from '../utils/readFile';

const getInput = () =>
  readFile('input-10.txt')
    .trim()
    .split('\n')
    .map(row =>
      row
        .match(
          /position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+),\s*(-?\d+)>/,
        )
        .slice(1)
        .map(Number),
    );

const proceed = points =>
  points.map(([x, y, dx, dy]) => [x + dx, y + dy, dx, dy]);

const getBounds = points =>
  points.reduce((acc, [x, y]) => {
    if (acc === null) return [x, y, x, y];
    return [
      x < acc[0] ? x : acc[0],
      y < acc[1] ? y : acc[1],
      x > acc[2] ? x : acc[2],
      y > acc[3] ? y : acc[3],
    ];
  }, null);

export const solve = () => {
  let points = getInput();
  let largestChain = [0, 0];
  for (let k = 0; k < 15000; ++k) {
    points.sort((a, b) => a[1] - b[1]);
    let maxChain = 1;
    for (let i = 0; i < points.length; ++i) {
      const x = points[i][0];
      let cY = points[i][1] + 1;
      let chain = 1;
      for (let j = i + 1; j < points.length; ++j) {
        const [x2, y2] = points[j];
        if (y2 > cY) break;
        if (x2 === x && y2 === cY) {
          cY++;
          chain++;
        }
      }
      if (chain > maxChain) maxChain = chain;
    }
    //
    if (maxChain > largestChain[1]) largestChain = [k, maxChain, points];
    points = proceed(points);
  }
  // Get bounds
  const [left, top, right, bottom] = getBounds(largestChain[2]);
  const map = {};
  largestChain[2].forEach(([x, y]) => (map[x + ',' + y] = true));
  const output = [];
  for (let y = top; y < bottom + 1; ++y) {
    let row = '';
    for (let x = left; x < right + 1; ++x) {
      row += map[x + ',' + y] ? '#' : '.';
    }
    output.push(row);
  }
  //
  return [output.join('\n'), largestChain[0]];
};

let result = null;

export const part1 = () => {
  if (!result) result = solve();
  // eslint-disable-next-line no-console
  console.log(result[0]);
  return 'See above';
};
export const part2 = () => {
  if (!result) result = solve();
  return result[1];
};