This commit is contained in:
Ben Ashton 2022-12-29 07:43:20 -07:00
parent 24c9bfc88e
commit 23bd9622a1
2 changed files with 5055 additions and 0 deletions

5000
20/input Normal file

File diff suppressed because it is too large Load Diff

55
20/solution.mjs Normal file
View File

@ -0,0 +1,55 @@
import { readFileSync } from 'node:fs';
const input = readFileSync('input', 'utf-8');
const numbers = input
.split('\n')
.filter(line => !!line.trim())
.map(num => parseInt(num));
function getNextPos(pos, move, len) {
if (move === 0) {
return pos;
} else if (move > 0) {
return (pos + move) % (len - 1);
} else {
return (len - 1) - ((len - 1 - pos + Math.abs(move)) % (len - 1));
}
}
function mix(numberArray, count=1) {
const sequence = numberArray.map((move, sequence) => ({move, sequence}));
for (let c = 0; c < count; c++) {
for (let i = 0; i < sequence.length; i++) {
const itemIndex = sequence.findIndex(item => item.sequence === i);
const item = sequence[itemIndex];
const newIndex = getNextPos(itemIndex, item.move, sequence.length);
sequence.splice(itemIndex, 1);
sequence.splice(newIndex, 0, item);
}
}
return sequence.map(s => s.move);
}
function getCoordinates(mixed) {
const zeroIndex = mixed.indexOf(0);
const coordinates = (
mixed[(zeroIndex + 1000) % (mixed.length)] +
mixed[(zeroIndex + 2000) % (mixed.length)] +
mixed[(zeroIndex + 3000) % (mixed.length)]
);
return coordinates;
}
const partOneMixed = mix(numbers);
const partOneCoords = getCoordinates(partOneMixed);
console.log('The sum of grove coordinates for part one is:', partOneCoords);
const partTwoNumbers = numbers.map(n => n * 811589153);
const partTwoMixed = mix(partTwoNumbers, 10);
const partTwoCoords = getCoordinates(partTwoMixed);
console.log('The sum of grove coordinates for part two is:', partTwoCoords);