Created
December 11, 2017 15:57
-
-
Save amwmedia/3a0d107c1e6b47e2327aa575bc1621ad to your computer and use it in GitHub Desktop.
Advent Of Code 2017 - Day 6
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
// input => [10, 3, 15, ...]; | |
function memoryBanks(input) { | |
const states = []; | |
let opCount = 0; | |
for (; !states.includes(input.join('|')) ;) { | |
states.push(input.join('|')); | |
const highestNum = Math.max.apply(null, input); | |
let idx = input.indexOf(highestNum); | |
let amt = input[idx]; | |
input[idx] = 0; | |
for (idx++; amt--; idx++) { | |
const wrappedIdx = (idx + input.length) % input.length; | |
input[wrappedIdx]++; | |
} | |
opCount++; | |
} | |
return opCount; | |
} |
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
// input => [10, 3, 15, ...]; | |
function memoryBanksLoopLength(input) { | |
const states = []; | |
let opCount = 0; | |
for (; !states.includes(input.join('|')) ;) { | |
states.push(input.join('|')); | |
const highestNum = Math.max.apply(null, input); | |
let idx = input.indexOf(highestNum); | |
let amt = input[idx]; | |
input[idx] = 0; | |
for (idx++; amt--; idx++) { | |
const wrappedIdx = (idx + input.length) % input.length; | |
input[wrappedIdx]++; | |
} | |
opCount++; | |
} | |
return opCount - states.indexOf(input.join('|')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment