-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11_pt1.js
62 lines (58 loc) · 1.26 KB
/
day11_pt1.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const input = require("fs")
.readFileSync("day11_input.txt")
.toString()
.split(",")
console.log(input)
let state = {
distance: 0,
hexCoord: [0, 0],
maxDistance: 0,
}
for (let i = 0; i < input.length; i++) {
state.hexCoord = moveDirection(input[i], state.hexCoord)
state.distance = findDistance(state.hexCoord)
if (i === 0) {
state.maxDistance = state.distance
} else {
if (state.distance > state.maxDistance) {
state.maxDistance = state.distance
}
}
}
console.log(state)
function moveDirection(direction, hexCoord) {
let updatedHexCoord = [...hexCoord]
switch (direction) {
case "n":
updatedHexCoord[1] += 2
break
case "ne":
updatedHexCoord[0] += 1
updatedHexCoord[1] += 1
break
case "se":
updatedHexCoord[0] += 1
updatedHexCoord[1] -= 1
break
case "s":
updatedHexCoord[1] -= 2
break
case "sw":
updatedHexCoord[0] -= 1
updatedHexCoord[1] -= 1
break
case "nw":
updatedHexCoord[0] -= 1
updatedHexCoord[1] += 1
}
return updatedHexCoord
}
function findDistance(hexCoord) {
let absX = Math.abs(hexCoord[0])
let absY = Math.abs(hexCoord[1])
if (absX > absY) {
return absX
} else {
return (absX + absY) / 2
}
}