Files
advent-of-code-2022/2021/01/solution.ts
2022-12-05 21:30:36 +01:00

44 lines
1.1 KiB
TypeScript

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));