You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.6 KiB
55 lines
1.6 KiB
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);
|
|
|