You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
609 B
JavaScript
23 lines
609 B
JavaScript
const fs = require("fs")
|
|
|
|
const input = fs.readFileSync("input.txt", "utf-8", err => {
|
|
err && console.error(err)
|
|
}).split("\n").map(line => line.split(" "))
|
|
|
|
let horizontalPosition = 0, depth = 0, aim = 0
|
|
|
|
for (let [direction, distance] of input){
|
|
distance = parseInt(distance)
|
|
switch(direction){
|
|
case "forward":
|
|
horizontalPosition += distance
|
|
depth += aim * distance
|
|
break
|
|
case "up":
|
|
aim -= distance
|
|
break
|
|
case "down":
|
|
aim += distance
|
|
}
|
|
}
|
|
console.log(horizontalPosition * aim, horizontalPosition * depth) |