Browse Source

Day 20

master
Ben Ashton 1 year ago
parent
commit
23bd9622a1
  1. 5000
      20/input
  2. 55
      20/solution.mjs

5000
20/input

File diff suppressed because it is too large Load Diff

55
20/solution.mjs

@ -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);
Loading…
Cancel
Save