diff --git a/day1-puzzle1.ts b/day1-puzzle1.ts index 59e997b..ca525bc 100644 --- a/day1-puzzle1.ts +++ b/day1-puzzle1.ts @@ -1,6 +1,6 @@ import fs from "node:fs"; -const input = fs.readFileSync("inputs/day1-puzzle1").toString(); +const input = fs.readFileSync("inputs/day1").toString(); const sort = (a: number, b: number) => a - b; diff --git a/day1-puzzle2.ts b/day1-puzzle2.ts new file mode 100644 index 0000000..fffbcd6 --- /dev/null +++ b/day1-puzzle2.ts @@ -0,0 +1,26 @@ +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); \ No newline at end of file