add Day2
This commit is contained in:
parent
9e7f91a0be
commit
0a4e8cde6e
61
Day2/Submarine.ts
Normal file
61
Day2/Submarine.ts
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
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());
|
||||||
1000
Day2/input.txt
Normal file
1000
Day2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user