2023-05-28 21:47:42 +02:00

45 lines
1.1 KiB
TypeScript

import { readLines } from "https://deno.land/std@0.177.1/io/read_lines.ts";
async function Part1() {
const file = await Deno.open("./Day1/input.txt");
const lines = readLines(file);
let last = +(await lines.next()).value;
let descends = 0;
for await (const depth of lines) {
if (+depth > last) {
descends++;
}
last = +depth;
}
return descends;
}
async function Part2(windowSize: number) {
const file = await Deno.open("./Day1/input.txt");
const lines = readLines(file);
const window: number[] = new Array(windowSize);
let descends = 0;
for (let i = 0; i < windowSize; i++) {
window[i] = +(await lines.next()).value;
}
let oldSum = window.reduce((x,y) => x+y);
for await (const depth of lines) {
const oldValue = window.shift() as number;
const newSum = oldSum - oldValue + +depth;
if (newSum > oldSum) {
descends++;
}
oldSum = newSum;
window.push(+depth);
}
return descends;
}
console.log("Part1: " + await Part1());
console.log("Part2: " + await Part2(3));