Created
December 23, 2020 18:51
-
-
Save warriordog/3a6808fcb280f91b34a1977953f6c9b5 to your computer and use it in GitHub Desktop.
Solution to Advent of Code 2020 Day 23 Part 2, using only an array and no linked list for performance.
This file contains 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
//const INPUT = [ 3, 8, 9, 1, 2, 5, 4, 6, 7 ]; // TEST input | |
const INPUT = [ 1, 6, 7, 2, 4, 8, 3, 5, 9 ]; // PROD input - expect 21986479838 | |
const MOVES = 10000000; | |
const cups = (function () { | |
const numbers = Array.from(INPUT); | |
// init circle | |
for (let idx = 0; idx < 1000000; idx++) { | |
let nextIdx = idx + 1; | |
if (nextIdx === 1000000) { | |
nextIdx = 0; | |
} | |
numbers[idx] = nextIdx; | |
} | |
// add input | |
for (let i = 0; i < INPUT.length; i++) { | |
const iNum = INPUT[i]; | |
const nextNum = INPUT[i + 1] || INPUT.length + 1; | |
numbers[iNum - 1] = nextNum - 1; | |
} | |
return numbers; | |
})(); | |
function playAllMoves() { | |
let currentCup = 0; | |
for (let i = 0; i < MOVES; i++) { | |
// get cups to move (and ends) | |
const rCup1 = cups[currentCup]; | |
const rCup2 = cups[rCup1]; | |
const rCup3 = cups[rCup2]; | |
// remove cups | |
cups[currentCup] = cups[rCup3]; | |
// find insertion point | |
let insertIdx = currentCup; | |
do { | |
insertIdx--; | |
if (insertIdx < 0) { | |
insertIdx = 999999; | |
} | |
} while (insertIdx === rCup1 || insertIdx === rCup2 || insertIdx === rCup3); | |
// insert | |
const afterInsertIdx = cups[insertIdx]; | |
cups[insertIdx] = rCup1; | |
cups[rCup3] = afterInsertIdx; | |
// select new current | |
currentCup = cups[currentCup]; | |
} | |
} | |
/* Part 2 */ | |
playAllMoves(); | |
const cup2Idx = cups[0]; | |
const cup2 = cup2Idx + 1; | |
const cup3Idx = cups[cup2Idx]; | |
const cup3 = cup3Idx + 1; | |
console.log(`Part 2: The final result is ${ cup2 } x ${ cup3 } = ${ cup2 * cup3}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment