aoc-2024/day1-puzzle2.ts

26 lines
560 B
TypeScript
Raw Permalink Normal View History

2024-12-01 05:01:55 -05:00
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) => {
rows.push(a * lists[1].filter(b => a === b).length);
})
const result = rows.reduce((prev, cur) => prev + cur);
console.log(result);