Last active
December 15, 2020 17:40
AoC 2020 Day 15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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