-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_2.js
More file actions
38 lines (34 loc) · 1.12 KB
/
Copy pathday_2.js
File metadata and controls
38 lines (34 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const fs = require('fs')
const updatePosition = (positionToUpdate, newPosition) => {
switch(newPosition.arrow) {
case 'forward':
positionToUpdate.x += newPosition.step
if (positionToUpdate.aim > 0) {
positionToUpdate.y += (newPosition.step * positionToUpdate.aim)
}
break
case 'down':
// positionToUpdate.y += newPosition.step
positionToUpdate.aim += newPosition.step
break
case 'up':
// positionToUpdate.y -= newPosition.step
positionToUpdate.aim -= newPosition.step
break
}
return positionToUpdate
}
fs.readFile('./inputs/2.txt', 'utf8', (err, response) => {
if (err) throw err
const data = response.split('\n').map((move) => {
const [arrow, step] = move.split(' ')
return { arrow, step: parseInt(step, 10) }
})
let position = { x:0, y:0, aim:0 }
data.forEach((move) => {
position = updatePosition(position, move)
})
result = position.x * position.y
console.log({position, result})
return result
})