Solutions for day two

This commit is contained in:
Eric Wagoner 2021-12-02 00:15:21 -05:00
parent a2292d251b
commit 71f2b7059c
2 changed files with 1054 additions and 0 deletions

54
02/code.js Normal file
View File

@ -0,0 +1,54 @@
const fs = require('fs');
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
// Part One
/*
let position = 0;
let depth = 0;
for(i in inputArray) {
const lineArray = inputArray[i].split(" ");
const direction = lineArray[0];
const distance = parseInt(lineArray[1]);
switch (direction) {
case "forward":
position = position + distance;
break;
case "up":
depth = depth - distance;
break;
case "down":
depth = depth + distance;
break;
}
}
const product = position * depth;
console.log(`${position} * ${depth} = ${product}`);
*/
// Part Two
let aim = 0;
let depth = 0;
let position = 0;
for(i in inputArray) {
const lineArray = inputArray[i].split(" ");
const direction = lineArray[0];
const distance = parseInt(lineArray[1]);
switch (direction) {
case "forward":
position = position + distance;
depth = depth + (aim * distance);
break;
case "up":
aim = aim - distance;
break;
case "down":
aim = aim + distance;
break;
}
}
const product = position * depth;
console.log(`${position} * ${depth} = ${product}`);

1000
02/input.txt Normal file

File diff suppressed because it is too large Load Diff