AdventOfCode/JS/day13/solution_a.mjs
Tobias Berger fe1d029ed4
JavaScript Day 13
Signed-off-by: Tobias Berger <tobi.berger13@gmail.com>
2022-12-01 13:26:33 +01:00

27 lines
730 B
JavaScript

import fs from "fs";
/**
* Main function for Puzzle A
*
* @param {string} data - Puzzle input as a single string.
* @returns {Promise<number>} The ID of the earliest bus to take, multiplied by the minutes until then.
*/
async function main(data) {
const lines = data.split(/\r?\n/);
const earliest = parseInt(lines[0]);
const buses = lines[1]
.split(",")
.map((val) => parseInt(val))
.filter((num) => !isNaN(num));
const departsAfter = buses
.map((id) => [id, id - (earliest % id)])
.sort((a, b) => a[1] - b[1]);
return departsAfter[0][0] * departsAfter[0][1];
}
fs.readFile("input", (err, data) => {
if (err) throw err;
main(data.toString()).then(console.log).catch(console.error);
});