Day 2
This commit is contained in:
6
src/day2/example.txt
Normal file
6
src/day2/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2
|
||||
34
src/day2/index.ts
Normal file
34
src/day2/index.ts
Normal 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
1000
src/day2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user