2021 - Day 1
This commit is contained in:
43
2021/01/solution.ts
Normal file
43
2021/01/solution.ts
Normal 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));
|
||||
Reference in New Issue
Block a user