-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.go
220 lines (174 loc) · 3.83 KB
/
day14.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
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
package main
import (
"bufio"
_ "embed"
"fmt"
"math"
"strconv"
"strings"
)
//go:embed input.txt
var input string
const WIDTH = 800
const HEIGHT = 180
func min(a int, b int) (int, int) {
if a < b {
return a, b
} else {
return b, a
}
}
type Window struct {
x1 int
y1 int
x2 int
y2 int
}
func (current *Window) Update(newX int, newY int) {
current.x1, _ = min(current.x1, newX)
_, current.x2 = min(newX, current.x2)
current.y1, _ = min(current.y1, newY)
_, current.y2 = min(newY, current.y2)
}
func ParseInput(matrix *[HEIGHT][WIDTH]byte) Window {
scanner := bufio.NewScanner(strings.NewReader(input))
scanner.Split(bufio.ScanLines)
var window Window = Window{math.MaxInt, math.MaxInt, math.MinInt, math.MinInt}
for scanner.Scan() {
line := scanner.Text()
var points []string = strings.Split(line, " -> ")
coord := strings.Split(points[0], ",")
currentX, _ := strconv.Atoi(coord[0])
currentY, _ := strconv.Atoi(coord[1])
window.Update(currentX, currentY)
for i := 1; i < len(points); i++ {
coord2 := strings.Split(points[i], ",")
nextX, _ := strconv.Atoi(coord2[0])
nextY, _ := strconv.Atoi(coord2[1])
window.Update(nextX, nextY)
startX, endX := min(currentX, nextX)
for x := startX; x <= endX; x++ {
matrix[currentY][x] = '#'
}
startY, endY := min(currentY, nextY)
for y := startY; y <= endY; y++ {
matrix[y][currentX] = '#'
}
currentX = nextX
currentY = nextY
}
}
matrix[0][500] = '+'
return window
}
func PrintMatrix(matrix *[HEIGHT][WIDTH]byte, window Window) {
for y := window.y1; y < window.y2+2; y++ {
for x := window.x1; x < window.x2; x++ {
if y == 0 && x == 500 {
fmt.Print("+")
continue
}
if matrix[y][x] == 0 {
fmt.Print(".")
} else {
fmt.Printf("%c", matrix[y][x])
}
}
fmt.Println()
}
}
func FallSand(srcx int, srcy int, matrix *[HEIGHT][WIDTH]byte, floorY int, window *Window) int {
step := 0
generationFlag := true
for generationFlag {
step++
sandx := srcx
sandy := srcy + 1
// filled up to source
if matrix[sandy][sandx] == 'o' {
generationFlag = false
break
}
keepMoving := true
for keepMoving {
//window.Update(sandx+1, sandy+1)
if sandy+1 == floorY {
generationFlag = false // leaking, stop generation!
break
}
if matrix[sandy+1][sandx] == 0 {
sandy++
continue
}
if matrix[sandy+1][sandx-1] == 0 {
sandy++
sandx--
continue
}
if matrix[sandy+1][sandx+1] == 0 {
sandx++
sandy++
continue
}
keepMoving = false
break
}
matrix[sandy][sandx] = 'o'
}
return step - 1
}
func FallSand2(srcx int, srcy int, matrix *[HEIGHT][WIDTH]byte, floorY int, window *Window) int {
step := 0
generationFlag := true
for generationFlag {
step++
sandx := srcx
sandy := srcy
// filled up to source
if matrix[sandy][sandx] == 'o' {
generationFlag = false
break
}
keepMoving := true
for keepMoving {
//window.Update(sandx+1, sandy+1)
if sandy+1 == floorY { // floor
break
}
if matrix[sandy+1][sandx] == 0 {
sandy++
continue
}
if matrix[sandy+1][sandx-1] == 0 {
sandy++
sandx--
continue
}
if matrix[sandy+1][sandx+1] == 0 {
sandx++
sandy++
continue
}
keepMoving = false
break
}
matrix[sandy][sandx] = 'o'
}
return step - 1
}
func Solve() (int, int) {
var matrix [HEIGHT][WIDTH]byte
var window Window = ParseInput(&matrix)
fmt.Println(window)
var matrixCopy [HEIGHT][WIDTH]byte
for y := 0; y < HEIGHT; y++ {
for x := 0; x < WIDTH; x++ {
matrixCopy[y][x] = matrix[y][x]
}
}
part1 := FallSand(500, 0, &matrix, HEIGHT, &window)
// PrintMatrix(&matrix, Window{459, 13, 529, 170})
part2 := FallSand2(500, 0, &matrixCopy, window.y2+2, &window)
// PrintMatrix(&matrixCopy, Window{329, 0, 672, 172})
return part1, part2 // 24 / 832, 140 / 27601
}