add Day1
This commit is contained in:
parent
d0bf5569e0
commit
9e7f91a0be
45
Day1/Sonar.ts
Normal file
45
Day1/Sonar.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
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));
|
||||||
2000
Day1/input.txt
Normal file
2000
Day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
11
deno.lock
generated
Normal file
11
deno.lock
generated
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "2",
|
||||||
|
"remote": {
|
||||||
|
"https://deno.land/std@0.177.1/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462",
|
||||||
|
"https://deno.land/std@0.177.1/bytes/concat.ts": "d26d6f3d7922e6d663dacfcd357563b7bf4a380ce5b9c2bbe0c8586662f25ce2",
|
||||||
|
"https://deno.land/std@0.177.1/bytes/copy.ts": "939d89e302a9761dcf1d9c937c7711174ed74c59eef40a1e4569a05c9de88219",
|
||||||
|
"https://deno.land/std@0.177.1/io/buf_reader.ts": "90a7adcb3638d8e1361695cdf844d58bcd97c41711dc6f9f8acc0626ebe097f5",
|
||||||
|
"https://deno.land/std@0.177.1/io/read_lines.ts": "baee9e35034f2fdfccf63bc24b7e3cb45aa1c1c5de26d178f7bcbc572e87772f",
|
||||||
|
"https://deno.land/std@0.177.1/types.d.ts": "220ed56662a0bd393ba5d124aa6ae2ad36a00d2fcbc0e8666a65f4606aaa9784"
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user