2024 day 1 deno

This commit is contained in:
Dimitri Lozeve 2024-12-01 10:16:42 +01:00
parent 2665bbbaaf
commit f68515f883

16
2024/day01/day01.ts Normal file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env -S deno run --allow-read
const input = await Deno.readTextFile("input");
const parsedInput = input
.split("\n")
.map((s) => s.split(" "))
.filter((l) => l.length == 2)
.map((l) => [parseInt(l[0]), parseInt(l[1])]);
const left = parsedInput.map((l) => l[0]).sort();
const right = parsedInput.map((l) => l[1]).sort();
console.log(
left.map((x, i) => Math.abs(x - right[i])).reduce((t, c) => t + c, 0),
);
const counts = Object.groupBy(right, (x) => x);
console.log(
left.map((n) => n * (counts[n]?.length || 0)).reduce((t, c) => t + c, 0),
);