-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17.go
More file actions
437 lines (377 loc) · 11.6 KB
/
day17.go
File metadata and controls
437 lines (377 loc) · 11.6 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package main
import (
"fmt"
"bufio"
"os"
// "os/exec"
"flag"
)
// Used to build a work list of water sources to be followed
type workListCoords struct {
done bool
xCoord int
yCoord int
}
type pointStruct struct {
xCoordStart int
xCoordEnd int
yCoordStart int
yCoordEnd int
yCountStart int
}
// Read the text file passed in by name into a array of strings
// Returns the array as the first return variable
func readLines(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func printStringArray(tempString []string) {
// Loop through the array and print each line
for i:= 0; i < len(tempString); i++ {
fmt.Println(tempString[i])
}
}
func print2DSlice(tempSlice [][]byte) {
for i := 0; i < len(tempSlice); i++ {
for j := 0; j < len(tempSlice[i]); j++ {
fmt.Printf("%c", tempSlice[i][j])
}
fmt.Printf("\n")
}
}
// func countWaterSquares
// Count the '~' and '|' squares in undergroundArea
func countWaterSquares(undergroundArea [][]byte, part byte, yCountStart int) int {
var count int = 0
for i := yCountStart; i < len(undergroundArea); i++ {
for j := 0; j < len(undergroundArea[i]); j++ {
if part == 'a' {
if undergroundArea[i][j] == '~' || undergroundArea[i][j] == '|' {
count++
}
} else {
if undergroundArea[i][j] == '~' {
count++
}
}
}
}
return count
}
func addWorkListItem(workList []workListCoords, workX int, workY int) []workListCoords {
var checkList bool = true
for i := 0; i < len(workList); i++ {
if workList[i].xCoord == workX && workList[i].yCoord == workY {
checkList = false
}
}
if checkList {
workList = append(workList, workListCoords{done: false, xCoord: workX, yCoord: workY})
}
return workList
}
// func: readInitialState
// takes an array of strings and breaks it into a 2D array of bytes
func readInitialState(coordData []pointStruct, undergroundArea [][]byte, springX int, springY int, minX int, minY int) {
for i := 0; i < len(undergroundArea); i++ {
for j := 0; j < len(undergroundArea[i]); j++ {
undergroundArea[i][j] = '.'
}
}
for i := 0; i < len(coordData); i++ {
if coordData[i].xCoordStart == coordData[i].xCoordEnd {
for y := (coordData[i].yCoordStart - minY); y <= (coordData[i].yCoordEnd - minY); y++ {
undergroundArea[y][coordData[i].xCoordStart-minX] = '#'
}
} else {
// must be 'y'
for x := (coordData[i].xCoordStart - minX); x < (coordData[i].xCoordEnd - minX); x++ {
undergroundArea[coordData[i].yCoordStart-minY][x] = '#'
}
}
}
undergroundArea[springY - minY][springX - minX] = '+'
}
// func scanInputForMaxMins
// Walks through the coordData array and works out the minimum and maximum values of X and Y
func scanInputForMaxMins(coordData []pointStruct, springX int, springY int) (int, int, int, int, int) {
var minX, maxX, minY, maxY, yCountStart int = 0, 0, 1000, 0, 0
minX = coordData[0].xCoordStart
maxX = coordData[0].xCoordEnd
minY = coordData[0].yCoordStart
maxY = coordData[0].yCoordEnd
for i := 0; i < len(coordData); i++ {
if coordData[i].xCoordStart < minX {
minX = coordData[i].xCoordStart
}
if coordData[i].xCoordEnd > maxX {
maxX = coordData[i].xCoordEnd + 5
}
if coordData[i].yCoordStart < minY {
minY = coordData[i].yCoordStart
}
if coordData[i].yCoordEnd > maxY {
maxY = coordData[i].yCoordEnd
}
}
yCountStart = minY
if springX < minX {
minX = springX
} else {
if springX > maxX {
maxX = springX
}
}
if springY < minY {
minY = springY
} else {
if springY > maxY {
maxY = springY
}
}
return minX, maxX, minY, maxY, yCountStart
}
// func processInputFile
// Returns a pointStruct array with the processed data in it
// Data is provided in the file as follows:
// x=495, y=2..7
// y=13, x=498..504
// So we need to deal with both a single x with multiple ys, and a single y with multiple xs
// In the struct, start and end will be the same for the single digit coord
func processInputFile(fileContents []string) []pointStruct {
var coordData []pointStruct
var a, b, c int
var coord1, coord2 byte
//var result int
for i := 0; i < len(fileContents); i++ {
fmt.Sscanf(fileContents[i], "%c=%d, %c=%d..%d\n", &coord1, &a, &coord2, &b, &c)
if coord1 == 'x' {
coordData = append(coordData, pointStruct{xCoordStart: a, xCoordEnd: a, yCoordStart: b, yCoordEnd: c})
} else {
// must be 'y'
coordData = append(coordData, pointStruct{xCoordStart: b, xCoordEnd: c, yCoordStart: a, yCoordEnd: a})
}
}
return coordData
}
func fillLine(undergroundArea [][]byte, workList []workListCoords, currentFlowX int, currentFlowY int, maxiMins pointStruct) (bool, []workListCoords) {
var x int = currentFlowX
var y int = currentFlowY
var leftEdge, rightEdge int = currentFlowX, currentFlowX
var isEnclosed bool = true
// Look for left extent
for {
if undergroundArea[y][x] != '#' && x >= maxiMins.xCoordStart {
if undergroundArea[y+1][x] == '#' || undergroundArea[y+1][x] == '~' {
x--
} else {
// Fallen off the edge to the left
isEnclosed = false
leftEdge = x
workList = addWorkListItem(workList, x, y)
break
}
} else {
if undergroundArea[y][x] == '#' {
// We have found a left edge
leftEdge = x+1
break
} else {
// No edge found
isEnclosed = false
break
}
}
}
// Look for right extent
x = currentFlowX
for {
if undergroundArea[y][x] != '#' && x <= maxiMins.xCoordEnd {
if undergroundArea[y+1][x] == '#' || undergroundArea[y+1][x] == '~' {
x++
} else {
// Fallen off the edge to the right
isEnclosed = false
rightEdge = x
workList = addWorkListItem(workList, x, y)
break
}
} else {
if undergroundArea[y][x] == '#' {
rightEdge = x-1
break
} else {
// No right edge found
isEnclosed = false
break
}
}
}
//fmt.Printf("Source X: %d Source Y: %d\n", currentFlowX, currentFlowY)
//fmt.Printf("Left Edge: %d Right Edge: %d\n", leftEdge, rightEdge)
if isEnclosed {
for i := leftEdge; i <= rightEdge; i++ {
undergroundArea[y][i] = '~'
}
} else {
for i := leftEdge; i <= rightEdge; i++ {
undergroundArea[y][i] = '|'
}
}
return isEnclosed, workList
}
// func letTheWaterFlow
// Handles how the water flows from the source x, y point until it either reaches the end of the undergroundArea or
// it splits into 2 sources itself
func letTheWaterFlow(undergroundArea [][]byte, workList []workListCoords, sourceX int, sourceY int, maxiMins pointStruct) (bool, []workListCoords) {
var currentFlowX, currentFlowY int = sourceX, sourceY
var loopThis bool = true
var firstVisit bool = true
var result bool
// forever loop as we're following water. We'll control the loop count ourselves
for loopThis {
switch undergroundArea[currentFlowY][currentFlowX] {
case '+': currentFlowY++
case '.':
// Check what's next:
// If free area then add '|' and continue
// If reached blocker
// check if line is fillable
// yes -> fill line with '~' and y--
// no -> find water sources (spills) and create new water sources in workList
// -> fill line with '|'
// -> mark this water source as complete
if currentFlowY >= maxiMins.yCoordEnd {
// We've reached the end of the grid so we're done
return true, workList
}
if undergroundArea[currentFlowY + 1][currentFlowX] == '.' {
undergroundArea[currentFlowY][currentFlowX] = '|'
currentFlowY++
} else {
// Make sure fillLine returns false if we have open ends otherwise this does the wrong thing
if undergroundArea[currentFlowY + 1][currentFlowX] == '#' || undergroundArea[currentFlowY + 1][currentFlowX] == '~' {
result, workList = fillLine(undergroundArea, workList, currentFlowX, currentFlowY, maxiMins)
if result {
currentFlowY--
} else {
return true, workList
}
} else {
if undergroundArea[currentFlowY + 1][currentFlowX] == '|' {
undergroundArea[currentFlowY][currentFlowX] = '|'
return true, workList
}
}
}
case '|':
if currentFlowX == sourceX && currentFlowY == sourceY && firstVisit {
currentFlowY++
firstVisit = false
} else {
if undergroundArea[currentFlowY + 1][currentFlowX] == '~' {
result, workList = fillLine(undergroundArea, workList, currentFlowX, currentFlowY, maxiMins)
if result {
currentFlowY--
} else {
// Found an edge
loopThis = false
break
}
}
}
case '~':
// This means that we're a water source that has reached the water created by a
// difference water source. We still have to do something, since we could be on the other side of a blocker and
// capable of filling where the other water source can't
if currentFlowX == sourceX && currentFlowY == sourceY {
currentFlowY++
} else {
if undergroundArea[currentFlowY + 1][currentFlowX] == '~' {
result, workList = fillLine(undergroundArea, workList, currentFlowX, currentFlowY, maxiMins)
if result {
currentFlowY--
} else {
loopThis = false
break
}
}
}
}
}
return true, workList
}
// func processWaterFlow
// Handles everything needed to work out the water flow (day 17 part A)
func processWaterFlow(fileName string, springX int, springY int, part byte) int {
var minX, maxX, minY, maxY, gridSizeX, gridSizeY, workX, workY, yCountStart int
var coordData []pointStruct
var workList []workListCoords
var letsLoopThis bool
var maxiMins pointStruct
var didWork bool
// Read contents of file into a string array
fileContents, _ := readLines(fileName)
coordData = processInputFile(fileContents)
minX, maxX, minY, maxY, yCountStart = scanInputForMaxMins(coordData, springX, springY)
maxiMins.xCoordStart = 0
maxiMins.xCoordEnd = maxX - minX
maxiMins.yCoordStart = 0
maxiMins.yCoordEnd = maxY - minY + 1
maxiMins.yCountStart = yCountStart
gridSizeX = (maxX - minX) + 1
gridSizeY = (maxY - minY) + 2
undergroundArea := make([][]byte, gridSizeY)
for i := 0; i < gridSizeY; i++ {
undergroundArea[i] = make([]byte, gridSizeX)
}
workList = make([]workListCoords, 0)
readInitialState(coordData, undergroundArea, springX, springY, minX, minY)
workList = addWorkListItem(workList, springX - minX, springY - minY)
letsLoopThis = true
for letsLoopThis {
didWork = false
// Loop through the list of work we have. This list is a list of water sources
for i := 0; i < len(workList); i++ {
if !workList[i].done {
workX = workList[i].xCoord
workY = workList[i].yCoord
letsLoopThis, workList = letTheWaterFlow(undergroundArea, workList, workX, workY, maxiMins)
workList[i].done = true
didWork = true
}
}
if !didWork {
letsLoopThis = false
}
}
// Print final water flow
print2DSlice(undergroundArea)
return countWaterSquares(undergroundArea, part, maxiMins.yCountStart)
}
// Main routine
func main() {
var springX, springY int = 0, 0
fileNamePtr := flag.String("file", "input1.txt", "A filename containing input strings")
flag.IntVar(&springX, "springx", 500, "x coord of the spring of water")
flag.IntVar(&springY, "springy", 0, "y coord of the spring of water")
execPartPtr := flag.String("part", "a", "Which part of day18 do you want to calc (a or b)")
flag.Parse()
switch *execPartPtr {
case "a":
fmt.Println("Part a - Number of water tiles:", processWaterFlow(*fileNamePtr, springX, springY, 'a'))
case "b":
fmt.Println("Part b - Number of still water tiles:", processWaterFlow(*fileNamePtr, springX, springY, 'b'))
default:
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
}
}