2022 - Day 1
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
const sample = await Deno.readTextFile("sample.txt");
|
||||
const input = await Deno.readTextFile("input.txt");
|
||||
|
||||
const solvePart1 = (data: string): number | undefined =>
|
||||
data
|
||||
.split("\n\n")
|
||||
.map((elf) =>
|
||||
elf
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((value) => parseInt(value, 10))
|
||||
.reduce((sum, value) => sum + value, 0)
|
||||
)
|
||||
.sort((a, b) => a - b)
|
||||
.pop();
|
||||
|
||||
console.log("Sample:", solvePart1(sample));
|
||||
console.log("Input", solvePart1(input));
|
||||
|
||||
const solvePart2 = (data: string): number =>
|
||||
data
|
||||
.split("\n\n")
|
||||
.map((elf) =>
|
||||
elf
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((value) => parseInt(value, 10))
|
||||
.reduce((sum, value) => sum + value, 0)
|
||||
)
|
||||
.sort((a, b) => b - a)
|
||||
.slice(0, 3)
|
||||
.reduce((sum, value) => sum + value, 0);
|
||||
|
||||
console.log("Sample:", solvePart2(sample));
|
||||
console.log("Input", solvePart2(input));
|
||||
Reference in New Issue
Block a user