Day Two - Received all stars | 02/12/2021
This commit is contained in:
5
Day 2/part-1/ExpectedTestResult.txt
Normal file
5
Day 2/part-1/ExpectedTestResult.txt
Normal file
@ -0,0 +1,5 @@
|
||||
final depth: 8
|
||||
final horizontal: 5
|
||||
|
||||
horizontal * depth
|
||||
5 * 8 = 40
|
1000
Day 2/part-1/data.txt
Normal file
1000
Day 2/part-1/data.txt
Normal file
File diff suppressed because it is too large
Load Diff
36
Day 2/part-1/nav_part1.js
Normal file
36
Day 2/part-1/nav_part1.js
Normal file
@ -0,0 +1,36 @@
|
||||
const fs = require("fs");
|
||||
const filePath = "data.txt";
|
||||
|
||||
fs.readFile( filePath, 'UTF-8', (err, data )=>{
|
||||
|
||||
let CommandsList = data.split("\n");
|
||||
let horizontalPosition = 0 , depth= 0;
|
||||
|
||||
|
||||
for( let i =0 ; i < CommandsList.length; i++){
|
||||
let [Command , amount] = CommandsList[i].split(" ");
|
||||
|
||||
console.log(Command);
|
||||
console.log(amount);
|
||||
|
||||
switch (Command){
|
||||
case 'forward':
|
||||
horizontalPosition += Number(amount);
|
||||
break;
|
||||
case 'down':
|
||||
depth += Number(amount);
|
||||
break;
|
||||
case 'up':
|
||||
depth -= Number(amount);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
console.log(`Final depth: ${depth}, Final Horizontal position: ${horizontalPosition}`);
|
||||
console.log( `The answer is: ${horizontalPosition * depth}` )
|
||||
|
||||
|
||||
|
||||
});
|
7
Day 2/part-1/testdata.txt
Normal file
7
Day 2/part-1/testdata.txt
Normal file
@ -0,0 +1,7 @@
|
||||
down 4
|
||||
down 1
|
||||
forward 5
|
||||
down 7
|
||||
down 3
|
||||
up 2
|
||||
up 5
|
Reference in New Issue
Block a user