Skip to content

Instantly share code, notes, and snippets.

@amwmedia
Created December 11, 2017 15:57
Show Gist options
  • Save amwmedia/3a0d107c1e6b47e2327aa575bc1621ad to your computer and use it in GitHub Desktop.
Save amwmedia/3a0d107c1e6b47e2327aa575bc1621ad to your computer and use it in GitHub Desktop.
Advent Of Code 2017 - Day 6
// 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;
}
// 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