Created
December 13, 2020 07:37
-
-
Save warriordog/a72cdec2137524fcf7989f1e3ebc68dc to your computer and use it in GitHub Desktop.
Solution to Advent of Code 2020 Day 3 Part 1
This file contains 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
const fs = require('fs'); | |
const inputParts = fs.readFileSync('day13-input.txt', 'utf-8').split('\r\n'); | |
const earliestTimestamp = parseInt(inputParts[0].trim()); | |
function findEarliestArrival(id) { | |
let wait = id; | |
while (wait < earliestTimestamp) { | |
wait += id; | |
} | |
return wait; | |
} | |
const busses = inputParts[1].split(',').filter(id => id !== 'x' && id.length > 0).map(id => parseInt(id.trim())).map(id => ({ | |
id, | |
earliestArrival: findEarliestArrival(id) | |
})); | |
const earliestBus = busses.reduce((earliest, current) => current.earliestArrival < earliest.earliestArrival ? current : earliest, busses[0]); | |
const wait = earliestBus.earliestArrival - earliestTimestamp; | |
console.log(`Part 1: ${ earliestBus.id * wait }`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment