-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.go
More file actions
279 lines (239 loc) · 7.65 KB
/
day03.go
File metadata and controls
279 lines (239 loc) · 7.65 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"AdventOfCode-go/advent2017/utils"
"fmt"
)
type coords struct {
x int
y int
}
func sumAdjacentSquares(spiralPattern map[coords]int, currentPosition coords) int {
var posValue, posSumTotal int
var posExists bool
posValue, posExists = spiralPattern[coords{currentPosition.x - 1, currentPosition.y - 1}]
if posExists {
posSumTotal += posValue
}
posValue, posExists = spiralPattern[coords{currentPosition.x, currentPosition.y - 1}]
if posExists {
posSumTotal += posValue
}
posValue, posExists = spiralPattern[coords{currentPosition.x + 1, currentPosition.y - 1}]
if posExists {
posSumTotal += posValue
}
posValue, posExists = spiralPattern[coords{currentPosition.x - 1, currentPosition.y}]
if posExists {
posSumTotal += posValue
}
posValue, posExists = spiralPattern[coords{currentPosition.x + 1, currentPosition.y}]
if posExists {
posSumTotal += posValue
}
posValue, posExists = spiralPattern[coords{currentPosition.x - 1, currentPosition.y + 1}]
if posExists {
posSumTotal += posValue
}
posValue, posExists = spiralPattern[coords{currentPosition.x, currentPosition.y + 1}]
if posExists {
posSumTotal += posValue
}
posValue, posExists = spiralPattern[coords{currentPosition.x + 1, currentPosition.y + 1}]
if posExists {
posSumTotal += posValue
}
return posSumTotal
}
func calcAdjacentValues(valueToCheck int, debug bool) int {
/*
As a stress test on the system, the programs here clear the grid and then
store the value 1 in square 1. Then, in the same allocation order as shown
above, they store the sum of the values in all adjacent squares, including
diagonals.
147 142 133 122 59
304 5 4 2 57
330 10 1 1 54
351 11 23 25 26
362 747 806---> ...
*/
const LEFT = 1
const UP = 2
const RIGHT = 3
const DOWN = 4
// The value to create in each position. Starts at 1
var currentPosValue int = 1
var movingDirection int = RIGHT
var currentPosition coords
spiralPattern := make(map[coords]int)
// Set where 1 starts from
currentPosition.x = 0
currentPosition.y = 0
spiralPattern[currentPosition] = currentPosValue
//for currentPosValue < valueToCheck {
for {
for movingDirection == RIGHT {
// Move one space to the right. Add the currentValue to that position
currentPosition.x++
currentPosValue = sumAdjacentSquares(spiralPattern, currentPosition)
spiralPattern[currentPosition] = currentPosValue
if currentPosValue > valueToCheck {
return currentPosValue
}
// If space ABOVE is empty then we start going upwards
_, posExists := spiralPattern[coords{currentPosition.x, currentPosition.y - 1}]
if !posExists {
movingDirection = UP
}
}
for movingDirection == UP {
// Move one space to UP. Add the currentValue to that position
currentPosition.y--
currentPosValue = sumAdjacentSquares(spiralPattern, currentPosition)
spiralPattern[currentPosition] = currentPosValue
if currentPosValue > valueToCheck {
return currentPosValue
}
// If space LEFT is empty then we start going left
_, posExists := spiralPattern[coords{currentPosition.x - 1, currentPosition.y}]
if !posExists {
movingDirection = LEFT
}
}
for movingDirection == LEFT {
// Move one space to LEFT. Add the currentValue to that position
currentPosition.x--
currentPosValue = sumAdjacentSquares(spiralPattern, currentPosition)
spiralPattern[currentPosition] = currentPosValue
if currentPosValue > valueToCheck {
return currentPosValue
}
// If space DOWN is empty then we start going down
_, posExists := spiralPattern[coords{currentPosition.x, currentPosition.y + 1}]
if !posExists {
movingDirection = DOWN
}
}
for movingDirection == DOWN {
// Move one space to DOWN. Add the currentValue to that position
currentPosition.y++
currentPosValue = sumAdjacentSquares(spiralPattern, currentPosition)
spiralPattern[currentPosition] = currentPosValue
if currentPosValue > valueToCheck {
return currentPosValue
}
// If space RIGHT is empty then we start going right
_, posExists := spiralPattern[coords{currentPosition.x + 1, currentPosition.y}]
if !posExists {
movingDirection = RIGHT
}
}
}
}
// valueToCheck is the value that we want to build the spiral to, then
// check the manhattan distance between it and 1
func calcSteps(valueToCheck int, debug bool) int {
/*
Each square on the grid is allocated in a spiral pattern starting at a location
marked 1 and then counting up while spiraling outward. For example, the first
few squares are allocated like this:
17 16 15 14 13
18 5 4 3 12
19 6 1 2 11
20 7 8 9 10
21 22 23---> ...
While this is very space-efficient (no squares are skipped), requested data must
be carried back to square 1 (the location of the only access port for this memory
system) by programs that can only move up, down, left, or right. They always
take the shortest path: the Manhattan Distance between the location of the data
and square 1.
*/
//var spiralPattern map[int,int]int
const LEFT = 1
const UP = 2
const RIGHT = 3
const DOWN = 4
// The value to create in each position. Starts at 1
var currentPosValue int = 1
var movingDirection int = RIGHT
var currentPosition coords
spiralPattern := make(map[coords]int)
// Set where 1 starts from
currentPosition.x = 0
currentPosition.y = 0
spiralPattern[currentPosition] = currentPosValue
for currentPosValue <= valueToCheck {
for movingDirection == RIGHT {
// Move one space to the right. Add the currentValue to that position
currentPosition.x++
currentPosValue++
spiralPattern[currentPosition] = currentPosValue
// If space ABOVE is empty then we start going upwards
_, posExists := spiralPattern[coords{currentPosition.x, currentPosition.y - 1}]
if !posExists {
movingDirection = UP
}
}
for movingDirection == UP {
// Move one space to UP. Add the currentValue to that position
currentPosition.y--
currentPosValue++
spiralPattern[currentPosition] = currentPosValue
// If space LEFT is empty then we start going left
_, posExists := spiralPattern[coords{currentPosition.x - 1, currentPosition.y}]
if !posExists {
movingDirection = LEFT
}
}
for movingDirection == LEFT {
// Move one space to LEFT. Add the currentValue to that position
currentPosition.x--
currentPosValue++
spiralPattern[currentPosition] = currentPosValue
// If space DOWN is empty then we start going down
_, posExists := spiralPattern[coords{currentPosition.x, currentPosition.y + 1}]
if !posExists {
movingDirection = DOWN
}
}
for movingDirection == DOWN {
// Move one space to DOWN. Add the currentValue to that position
currentPosition.y++
currentPosValue++
spiralPattern[currentPosition] = currentPosValue
// If space RIGHT is empty then we start going right
_, posExists := spiralPattern[coords{currentPosition.x + 1, currentPosition.y}]
if !posExists {
movingDirection = RIGHT
}
}
}
if debug {
fmt.Println(spiralPattern)
}
var resultPosition coords
for i, j := range spiralPattern {
if j == valueToCheck {
resultPosition = i
}
}
if debug {
fmt.Println(resultPosition)
}
return utils.ManhattanDistance2D(0, 0, resultPosition.x, resultPosition.y)
}
func solveSteps(inputData int, part byte, debug bool) int {
if part == 'a' {
return calcSteps(inputData, debug)
} else {
return calcAdjacentValues(inputData, debug)
}
}
// Main routine
func main() {
_, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", solveSteps(368078, execPart, debug))
}
}