Skip to content

Instantly share code, notes, and snippets.

@p-a
Last active December 15, 2020 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p-a/989bd6c0421086228e631b0414016258 to your computer and use it in GitHub Desktop.
Save p-a/989bd6c0421086228e631b0414016258 to your computer and use it in GitHub Desktop.
AoC 2020 Day 15
export const part1 = (numbers, target = 2020) => {
const idx = numbers.reduce((map, num, i) => map.set(num, i + 1), new Map());
let turn = numbers.length + 1;
let num = 0; // initial last number was unique for my input and test cases, so we will add 0 always
let last;
while (turn < target) {
last = idx.get(num) || 0; // prev turn for num or 0 for unseen
idx.set(num, turn); // set latest turn for num
num = last && turn - last; // age if seen, else 0
turn++; // next turn
}
return num;
};
export const part2 = numbers => part1(numbers, 30000000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment