-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (158 loc) · 3.16 KB
/
main.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
const north = 0
const east = 90
const south = 180
const west = 270
type step struct {
order string
steps int
}
type ferry struct {
x int
y int
direction int
}
type coord struct {
x int
y int
}
func (f *ferry) move(direction int, distance int) {
switch direction {
case north:
f.y += distance
case east:
f.x += distance
case west:
f.x -= distance
case south:
f.y -= distance
}
}
func (f *ferry) turn(angle int) {
f.direction = f.direction + angle
if f.direction < 0 {
f.direction += 360
}
if f.direction >= 360 {
f.direction -= 360
}
}
func main() {
dataAsString := load("sample_input.txt")
fmt.Println("step1 (sample) -->", step1(dataAsString))
fmt.Println("step2 (sample) -->", step2(dataAsString))
dataAsString = load("puzzle_input.txt")
fmt.Println("step1 -->", step1(dataAsString))
fmt.Println("step2 -->", step2(dataAsString))
}
func stringToSteps(dataAsString string) []step {
steps := []step{}
for _, line := range strings.Split(dataAsString, "\n") {
currentStep := step{order: string(line[0]), steps: atoi(line[1:])}
steps = append(steps, currentStep)
}
return steps
}
func wayPointTurn(x, y, angle int) (int, int) {
if angle < 0 {
angle = 360 + angle
}
switch angle {
case 90:
return y, x * -1
case 180:
return x * -1, y * -1
case 270:
return y * -1, x
}
return x, y
}
func step2(dataAsString string) int {
steps := stringToSteps(dataAsString)
boat := coord{x: 0, y: 0}
waypoint := coord{x: 10, y: 1}
for _, step := range steps {
switch step.order {
case "F":
boat.x = boat.x + waypoint.x*step.steps
boat.y = boat.y + waypoint.y*step.steps
case "L":
waypoint.x, waypoint.y = wayPointTurn(waypoint.x, waypoint.y, step.steps*-1)
case "R":
waypoint.x, waypoint.y = wayPointTurn(waypoint.x, waypoint.y, step.steps)
case "N":
waypoint.y += step.steps
case "E":
waypoint.x += step.steps
case "W":
waypoint.x -= step.steps
case "S":
waypoint.y -= step.steps
}
// fmt.Println("--> ", step)
// fmt.Println("boat", boat)
// fmt.Println("waypoint", waypoint)
// fmt.Println()
}
return abs(boat.x) + abs(boat.y)
}
func step1(dataAsString string) int {
steps := stringToSteps(dataAsString)
boat := ferry{x: 0, y: 0, direction: east}
for _, step := range steps {
switch step.order {
case "F":
boat.move(boat.direction, step.steps)
case "L":
boat.turn(step.steps * -1)
case "R":
boat.turn(step.steps)
case "N":
boat.move(north, step.steps)
case "E":
boat.move(east, step.steps)
case "W":
boat.move(west, step.steps)
case "S":
boat.move(south, step.steps)
}
}
return abs(boat.x) + abs(boat.y)
}
// ----
func abs(i int) int {
if i > 0 {
return i
}
return i * -1
}
func exist(array []int, item int) bool {
for _, element := range array {
if element == item {
return true
}
}
return false
}
func load(filename string) string {
text, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Print(err)
}
return string(text)
}
func atoi(line string) int {
number, err := strconv.Atoi(line)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return number
}