Unable to load image

The luckiest of days, Day 13!!

Let's fricking gooo!

17
Jump in the discussion.

No email address required.

import * as fs from 'fs';

enum E {True=-1, Undef=0, False=1};
type T = number | (number|T)[]
const packets: T[][] = [];
const compare = (l: T, r: T): E => {
  if (typeof l === 'number' && typeof r === 'number')
    return l === r ? E.Undef : l < r ? E.True : E.False;
  const ll = (typeof l === 'number') ? [l] : l;
  const rl = (typeof r === 'number') ? [r] : r;
  const ret = ll.reduce((p: E, li, i) => {
    if (p) return p;
    const ri = rl[i];
    if (typeof ri === 'undefined') return E.False;
    const cmp = compare(li, ri);
    if (cmp) return cmp;
    return E.Undef;
  }, E.Undef);
  if (ret) return ret;
  else if (ll.length < rl.length) return E.True;
  else return E.Undef;
}
console.log(
  fs.readFileSync(process.argv[2], 'utf-8').split('\n\n').reduce((p, lines,i) => {
    const line = lines.split('\n');
    const [l,r] = line.map(s => JSON.parse(s));
    packets.push(l, r);
    const ret = compare(l, r);
    return p + (ret===E.True ? i+1 : 0) 
  }, 0
));
packets.sort(compare);
console.log(
  packets.reduce((prev, p, i) =>
  prev * (JSON.stringify(p).match(/^\[\[[2,6]\]\]$/) ? (i+1) : 1)
, 1)
);
Jump in the discussion.

No email address required.



Now playing: Donkey Kong Country Theme (DKC).mp3

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