27 lines
No EOL
587 B
TypeScript
27 lines
No EOL
587 B
TypeScript
import fs from "node:fs";
|
|
|
|
const input = fs.readFileSync("inputs/day1").toString();
|
|
|
|
const sort = (a: number, b: number) => a - b;
|
|
|
|
const lists: number[][] = [];
|
|
|
|
input.split("\n").forEach(line => {
|
|
line.split(/\s+/g).forEach((i, list) => {
|
|
lists[list] = lists[list] || [];
|
|
lists[list].push(Number(i));
|
|
})
|
|
})
|
|
|
|
lists.forEach(i => i.sort(sort));
|
|
|
|
const rows: number[] = [];
|
|
|
|
lists[0].forEach((a, i) => {
|
|
const nums = [a, lists[1][i]].sort(sort);
|
|
rows.push(nums[1] - nums[0]);
|
|
})
|
|
|
|
const result = rows.reduce((prev, cur) => prev + cur);
|
|
|
|
console.log(result); |