Skip to content

Instantly share code, notes, and snippets.

@siberex
Last active December 6, 2020 21:52
Show Gist options
  • Save siberex/343683d05e067238949037f339d66af2 to your computer and use it in GitHub Desktop.
Save siberex/343683d05e067238949037f339d66af2 to your computer and use it in GitHub Desktop.
Advent of code 2019

1: The Tyranny of the Rocket Equation

let input = await fetch('https://adventofcode.com/2019/day/1/input').then(r => r.text());
input = input.split('\n').filter(Boolean);

// Part 1
input.reduce((acc, curr) => {
    const res = (parseInt(curr) / 3 | 0) - 2;
    return acc + BigInt(res);
}, 0n);

// Part 2
function fuel(n) {
    n = parseInt(n);
    n = (n / 3 | 0) - 2;
    return n <= 0 ? 0 : n + fuel(n);
}
input.reduce((acc, curr) => {
    return acc + BigInt(fuel(curr));
}, 0n);

4: Secure Container

const input = '234208-765869';
const [from, to] = input.split('-').map(v => parseInt(v));

// Part 1
const reDuplicate = /([0-9])\1{1}/;
function test(n) {
    const nStr = n.toString();
    if (!reDuplicate.test(nStr)) return false;
    // Going from left to right, the digits never decrease
    return nStr.split('')
        .map(x => x | 0)
        .reduce(
            (acc, curr, i, digits) =>
                digits[i - 1]
                    ? (curr >= digits[i - 1]) && acc
                    : acc,
            true
        );
}
let count = 0;
for (let i = from; i < to; i++) {
    count += test(i);
}

// Part 2
function test(n) {
    const nStr = n.toString();
    // Two adjacent matching digits are not part of a larger group of matching digits
    const doubles = nStr.match(/([0-9])\1+/g)?.filter(m => m.length === 2);
    if (!doubles || !doubles.length) return false;

    // Going from left to right, the digits never decrease
    return nStr.split('')
        .map(x => x | 0)
        .reduce(
            (acc, curr, i, digits) =>
                digits[i - 1]
                    ? (curr >= digits[i - 1]) && acc
                    : acc,
            true
        );
}
let count = 0;
for (let i = from; i < to; i++) {
    count += test(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment