AdventOfCode2021/Day2/Submarine.ts
2023-05-29 09:17:12 +02:00

61 lines
1.4 KiB
TypeScript

import { readLines } from "https://deno.land/std@0.177.1/io/read_lines.ts";
async function Part1() {
const file = await Deno.open("./Day2/input.txt");
let depth = 0;
let position = 0;
for await (const line of readLines(file)) {
const commands = line.split(" ");
switch(commands[0]) {
case "forward": {
position += +commands[1];
break;
}
case "down": {
depth += +commands[1];
break;
}
case "up": {
depth -= +commands[1];
break;
}
}
}
return position * depth;
}
async function Part2() {
const file = await Deno.open("./Day2/input.txt");
let depth = 0;
let position = 0;
let aim = 0;
for await (const line of readLines(file)) {
const commands = line.split(" ");
switch(commands[0]) {
case "forward": {
position += +commands[1];
depth += aim * +commands[1];
break;
}
case "down": {
aim += +commands[1];
break;
}
case "up": {
aim -= +commands[1];
break;
}
}
}
return position * depth;
}
console.log("Part1: " + await Part1());
console.log("Part1: " + await Part2());