36 lines
863 B
JavaScript
36 lines
863 B
JavaScript
|
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}` )
|
||
|
|
||
|
|
||
|
|
||
|
});
|