This commit is contained in:
Nathan van Ofwegen
2021-12-02 14:01:44 +01:00
parent 2d7c7f8d0d
commit 066cf2a37a
6 changed files with 1316 additions and 5 deletions

6
src/day2/example.txt Normal file
View File

@@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

34
src/day2/index.ts Normal file
View File

@@ -0,0 +1,34 @@
import path from 'path';
import {readFile} from "../utils";
let input = readFile(path.resolve(__dirname, 'input.txt')).map((x: any) => x.split(' '));
let forward = 0;
let depth = 0;
const add = (command: any) => {
if (command[0] === 'forward') forward += Number(command[1]);
else if (command[0] === 'down') depth += Number(command[1]);
else depth -= Number(command[1]);
}
input.map(add)
console.log("Part 1", forward * depth)
forward = 0;
depth = 0;
let aim = 0;
const execute = (command: any) => {
if (command[0] === 'down') aim += Number(command[1]);
else if (command[0] === 'up') aim -= Number(command[1]);
else {
forward += Number(command[1])
depth += Number(command[1]) * aim
}
}
input.map(execute)
console.log("Part 2", forward * depth)

1000
src/day2/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
import * as fs from 'fs'
export const readFile = (file: string): any => {
fs.readFileSync(file, {encoding: "utf-8"}).split("\r\n");
return fs.readFileSync(file, {encoding: "utf-8"}).split("\r\n");
}