2024 day 1 deno
This commit is contained in:
parent
2665bbbaaf
commit
f68515f883
1 changed files with 16 additions and 0 deletions
16
2024/day01/day01.ts
Normal file
16
2024/day01/day01.ts
Normal 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),
|
||||
);
|
Loading…
Add table
Add a link
Reference in a new issue