-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday12b.go
More file actions
134 lines (115 loc) · 2.57 KB
/
day12b.go
File metadata and controls
134 lines (115 loc) · 2.57 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
)
// Abs returns the absolute value of x.
func intAbs(x int) int {
if x < 0 {
return -x
}
return x
}
// Returns: newDirection, newX, newY
func moveWaypoint(command byte, moveAmount int, startX int, startY int) (int, int) {
var newX, newY int
newX = startX
newY = startY
switch command {
case 'N': // Move North. Direction stays the same. North is negative Y
newY = startY - moveAmount
break
case 'S': // Move South. Direction stays the same. South is positive Y
newY = startY + moveAmount
break
case 'E': // Move East. Direction stays the same. East is positive X
newX = startX + moveAmount
break
case 'W': // Move West. Direction stays the same. West is negative X
newX = startX - moveAmount
break
default:
panic("Duff direction command given")
}
return newX, newY
}
// Return: shipX, shipY position
func moveShip(amount int, shipX int, shipY int, wayptX int, wayptY int) (int, int) {
shipX += wayptX * amount
shipY += wayptY * amount
return shipX, shipY
}
// Rotate the way point around the ship
// Return: wayptX, wayptY Waypoint position
func rotateWayPoint(command byte, amount int, shipX int, shipY int, wayptX int, wayptY int) (int, int) {
var newX, newY int
if command == 'R' {
switch amount {
case 90:
newX = -wayptY
newY = wayptX
break
case 180:
newX = -wayptX
newY = -wayptY
break
case 270:
newX = wayptY
newY = -wayptX
break
default:
panic("Bad rotation in rotateWayPoint")
}
return newX, newY
}
// Rotate Left
switch amount {
case 90:
newX = wayptY
newY = -wayptX
break
case 180:
newX = -wayptX
newY = -wayptY
break
case 270:
newX = -wayptY
newY = wayptX
break
default:
panic("Bad rotation in rotateWayPoint")
}
return newX, newY
}
func calcShipMovementB(filename string, part byte, debug bool) int {
var wayptX, wayptY int = 10, -1
var shipX, shipY int = 0, 0
var startX, startY int = 0, 0
var command byte
var amount int
puzzleInput, _ := readFile(filename)
for _, line := range puzzleInput {
fmt.Sscanf(line, "%c%d", &command, &amount)
switch command {
case 'N':
fallthrough
case 'S':
fallthrough
case 'E':
fallthrough
case 'W':
wayptX, wayptY = moveWaypoint(command, amount, wayptX, wayptY)
break
case 'F':
shipX, shipY = moveShip(amount, shipX, shipY, wayptX, wayptY)
break
case 'L':
fallthrough
case 'R':
wayptX, wayptY = rotateWayPoint(command, amount, shipX, shipY, wayptX, wayptY)
break
default:
panic("Duff commands given")
}
}
return manhattanDistance2D(startX, startY, shipX, shipY)
}