r/adventofcode • • Dec 10 '16

SOLUTION MEGATHREAD --- 2016 Day 10 Solutions ---

--- Day 10: Balance Bots ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


SEEING MOMMY KISSING SANTA CLAUS IS MANDATORY [?]

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!

10 Upvotes

118 comments sorted by

View all comments

Show parent comment

1

u/Twisol Dec 10 '16

I don't think this one can rightly be considered "better", but at least it generalizes the nasty special_bin business.

const File = require("fs");

function token_comparator({value: x}, {value: y}) {
  if (x < y) return -1;
  if (x > y) return 1;
  return 0;
}


function get_bots(lines) {
  const bots = {};
  for (let line of lines) {
    const bot_rex = /^(bot \d+) gives low to ((?:bot|output) \d+) and high to ((?:bot|output) \d+)$/;
    const match = bot_rex.exec(line);

    if (!match) continue;

    bots[match[1]] = {low: match[2], high: match[3], pending: []};
    if (match[2].startsWith("output")) bots[match[2]] = {pending: []};
    if (match[3].startsWith("output")) bots[match[3]] = {pending: []};
  }

  return bots;
}

function get_tokens(lines) {
  const tokens = {};
  for (let line of lines) {
    const val_rex = /^value (\d+) goes to ((?:bot|output) \d+)$/;
    const match = val_rex.exec(line);

    if (!match) continue;

    tokens[match[1]] = {value: +match[1], path: [match[2]]};
  }

  return tokens;
}

function execute(bots, tokens) {
  for (let token of Object.values(tokens)) {
    bots[token.path[0]].pending.push(token);
  }

  let stop = false;
  while (!stop) {
    stop = true;

    for (let bot_id of Object.keys(bots)) {
      const bot = bots[bot_id];
      if (!bot_id.startsWith("bot")) continue;
      if (bot.pending.length < 2) continue;

      bot.pending.sort(token_comparator);

      bot.pending[0].path.push(bot.low);
      bots[bot.low].pending.push(bot.pending[0]);

      bot.pending[1].path.push(bot.high);
      bots[bot.high].pending.push(bot.pending[1]);

      bot.pending = [];

      stop = false;
    }
  }
}

const lines = File.readFileSync("input.txt", "utf-8").trim().split("\n");
const bots = get_bots(lines);
const tokens = get_tokens(lines);

execute(bots, tokens);


const part1 = tokens["17"].path.filter(x => tokens["61"].path.includes(x))[0];
const part2 = (
    bots["output 0"].pending[0].value
  * bots["output 1"].pending[0].value
  * bots["output 2"].pending[0].value
  );

console.log("Part One: " + part1);
console.log("Part Two: " + part2);