Day 1 in deno wooo
This commit is contained in:
commit
3234d984fd
5 changed files with 1044 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
**/input.txt
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"deno.enable": true
|
||||
}
|
1000
day1/input.txt
Normal file
1000
day1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
16
day1/part1.ts
Normal file
16
day1/part1.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
const input = await Deno.readTextFile("./input.txt").then(
|
||||
(f) =>
|
||||
f.split("\n").reduce<[number[], number[]]>(
|
||||
(
|
||||
acc,
|
||||
cur,
|
||||
) => [[...acc[0], +cur.split(" ")[0]], [
|
||||
...acc[1],
|
||||
+cur.split(" ")[1],
|
||||
]],
|
||||
[[], []],
|
||||
),
|
||||
).then(([first, second]) => [first.toSorted(), second.toSorted()]).then((
|
||||
[first, second],
|
||||
) => first.reduce((acc, cur, i) => acc + Math.abs(cur - second[i]), 0));
|
||||
console.log("Answer = " + input);
|
24
day1/part2.ts
Normal file
24
day1/part2.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
const input = await Deno.readTextFile("./input.txt").then(
|
||||
(f) =>
|
||||
f.split("\n").reduce<[number[], number[]]>(
|
||||
(
|
||||
acc,
|
||||
cur,
|
||||
) => [[...acc[0], +cur.split(" ")[0]], [
|
||||
...acc[1],
|
||||
+cur.split(" ")[1],
|
||||
]],
|
||||
[[], []],
|
||||
),
|
||||
).then(([first, second]) => [
|
||||
first,
|
||||
second.reduce((acc, cur) => {
|
||||
if (acc[cur] !== undefined) acc[cur]++;
|
||||
else acc[cur] = 1;
|
||||
|
||||
return acc;
|
||||
}, {} as Record<number, number>),
|
||||
]).then(([first, map]) =>
|
||||
first.reduce((acc, cur) => acc + ((map[cur] ?? 0) * cur), 0)
|
||||
);
|
||||
console.log("Answer = " + input);
|
Loading…
Reference in a new issue