Day Two - Received all stars | 02/12/2021
This commit is contained in:
4
Day 2/part-2/ExpectedTestResult.txt
Normal file
4
Day 2/part-2/ExpectedTestResult.txt
Normal file
@ -0,0 +1,4 @@
|
||||
aim:
|
||||
horizontal:
|
||||
depth:
|
||||
|
1000
Day 2/part-2/data.txt
Normal file
1000
Day 2/part-2/data.txt
Normal file
File diff suppressed because it is too large
Load Diff
38
Day 2/part-2/nav_part2.js
Normal file
38
Day 2/part-2/nav_part2.js
Normal file
@ -0,0 +1,38 @@
|
||||
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, aim = 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);
|
||||
depth += aim * Number(amount) ;
|
||||
break;
|
||||
case 'down':
|
||||
aim += Number(amount);
|
||||
break;
|
||||
case 'up':
|
||||
aim -= Number(amount);
|
||||
break;
|
||||
|
||||
}
|
||||
console.log(`Final Horizontal position: ${horizontalPosition}, Final depth: ${depth}, aim; ${aim}`);
|
||||
|
||||
}
|
||||
|
||||
console.log(`Final Horizontal position: ${horizontalPosition}, Final depth: ${depth}`);
|
||||
console.log( `The answer is: ${horizontalPosition * depth}` )
|
||||
|
||||
|
||||
|
||||
});
|
6
Day 2/part-2/testdata.txt
Normal file
6
Day 2/part-2/testdata.txt
Normal file
@ -0,0 +1,6 @@
|
||||
forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2
|
Reference in New Issue
Block a user