Unable to load image

ADVENT OF CODE DAY 11: MONKEY BUSINESS :marseymonke:

Those FRICKING monkeys stole your stuff! Get it back! :soycry: :!marseymonke:

(Also, remember to leave a comment if you did something interesting :marseylove:)

35
Jump in the discussion.

No email address required.

import * as fs from 'fs'
const monkeys: {
  items: number[],
  op: (old: number) => number,
  test: (num: number) => number,
  n: number,
}[] = [];
let lsd = 1;
fs.readFileSync(process.argv[2], 'utf-8').split('\n\n').forEach((ls) => {
  const l = ls.split('\n');
  const opStr = l[2].split('=')[1] || '';
  const d = Number(l[3].match(/\d+/)?.[0]) || 0;
  if (lsd % d) lsd *= d;
  const tM =  Number(l[4].match(/\d+/)?.[0]) || 0;
  const fM = Number(l[5].match(/\d+/)?.[0]) || 0;
  monkeys.push({
    items: l[1].match(/\d+/g)?.map(Number) || [],
    op: (old: number) => Number(eval(opStr.replace(/old/g, String(old)))),
    test: (num: number) => (num % d) ? fM : tM,
    n: 0,
  });
});
const round = () => monkeys.forEach(monk => {
  const items = monk.items;
  monk.items = [];
  items.forEach(w => {
    const newW = monk.op(w) % lsd;
    const newM = monk.test(newW);
    monk.n++;
    monkeys[newM].items.push(newW);
  })
});
for (let i = 0; i < 10000; i++) round();
console.log(monkeys.sort((m1, m2) => m2.n-m1.n).slice(0,2).reduce((p, m) => p * m.n, 1));
Jump in the discussion.

No email address required.



Now playing: DK's Treehouse (DK64).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.