2021 - Day 1

This commit is contained in:
2022-12-05 21:30:36 +01:00
parent 83d5f3758e
commit 39afd78853
5 changed files with 2132 additions and 0 deletions

43
2021/01/solution.ts Normal file
View File

@@ -0,0 +1,43 @@
const sample = await Deno.readTextFile("sample.txt");
const input = await Deno.readTextFile("input.txt");
const solvePart1 = (data: string): number | undefined =>
data
.split("\n")
.filter(Boolean)
.map(parseFloat)
.reduce(
([count, lastValue], value, index) => [
count + (index > 0 && value > lastValue ? 1 : 0),
value,
],
[0, 0]
)
.shift();
console.log("Sample:", solvePart1(sample));
console.log("Input", solvePart1(input));
const solvePart2 = (data: string): number | undefined =>
data
.split("\n")
.filter(Boolean)
.map(parseFloat)
.reduce(
(windows, _, index, values) =>
index >= values.length - 2
? windows
: [...windows, values[index] + values[index + 1] + values[index + 2]],
[] as number[]
)
.reduce(
([count, lastValue], value, index) => [
count + (index > 0 && value > lastValue ? 1 : 0),
value,
],
[0, 0]
)
.shift();
console.log("Sample:", solvePart2(sample));
console.log("Input", solvePart2(input));