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

32 lines
644 B
JavaScript

import fs from "fs";
/**
* Main function for Puzzle A
*
* @param {string} data - Puzzle input as a single string.
*/
async function main(data) {
const adapters = data
.split(/\r?\n/)
.map((line) => parseInt(line))
.sort((a, b) => a - b);
/** @type {number[]} */
const diffs = [];
let curNum = 0;
while (adapters.length > 0) {
const delta = adapters[0] - curNum;
diffs[delta] = (diffs[delta] ?? 0) + 1;
curNum = adapters.shift();
}
return diffs[1] * (diffs[3] + 1);
}
fs.readFile("input", (err, data) => {
if (err) throw err;
main(data.toString()).then(console.log).catch(console.error);
});