-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.go
229 lines (198 loc) · 3.91 KB
/
14.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
221
222
223
224
225
226
227
228
229
package main
import (
"fmt"
"strconv"
"strings"
)
type MapPoint int
const (
Air MapPoint = iota
Rock
Sand
)
type AOC202214SandMap struct {
// Point[y][x]
Point map[int]map[int]MapPoint
MaxY int
MinX int
MaxX int
Infinite bool
}
func (sm *AOC202214SandMap) DrawMap() string {
rtn := []string{}
yMax := sm.MaxY
if !sm.Infinite {
yMax += 2
}
for y := 0; y <= yMax; y++ {
line := ""
for x := sm.MinX; x <= sm.MaxX; x++ {
if x == 500 && y == 0 {
line += "+"
continue
}
pt := sm.GetPoint(x, y)
if pt == Air {
line += "."
} else if pt == Rock {
line += "#"
} else if pt == Sand {
line += "o"
}
}
rtn = append(rtn, line)
}
return strings.Join(rtn, "\n")
}
func (sm *AOC202214SandMap) SetPoint(x, y int, pt MapPoint) {
if sm.Point[y] == nil {
sm.Point[y] = make(map[int]MapPoint, 0)
}
sm.Point[y][x] = pt
if x > sm.MaxX {
sm.MaxX = x
} else if x < sm.MinX {
sm.MinX = x
}
}
func (sm *AOC202214SandMap) GetPoint(x, y int) MapPoint {
if !sm.Infinite && y == sm.MaxY+2 {
return Rock
}
if sm.Point[y] == nil {
sm.Point[y] = make(map[int]MapPoint)
return Air
}
elem, ok := sm.Point[y][x]
if !ok {
return Air
}
return elem
}
func (sm *AOC202214SandMap) AddSand() bool {
xPos, yPos := 500, 0
for {
if sm.Infinite {
// check if it fell off
if yPos > sm.MaxY {
return false
}
}
// check below
if sm.GetPoint(xPos, yPos+1) == Air {
yPos += 1
continue
}
// check below & left
if sm.GetPoint(xPos-1, yPos+1) == Air {
xPos -= 1
yPos += 1
continue
}
if sm.GetPoint(xPos+1, yPos+1) == Air {
xPos += 1
yPos += 1
continue
}
sm.SetPoint(xPos, yPos, Sand)
if xPos == 500 && yPos == 0 {
return false
}
return true
}
}
func (sm *AOC202214SandMap) ParseRocks(input string) error {
for _, line := range strings.Split(input, "\n") {
points := [][]int{}
for _, part := range strings.Split(line, " -> ") {
coord := strings.Split(part, ",")
coordX, err := strconv.Atoi(coord[0])
if err != nil {
return fmt.Errorf("can't parse coordinate %s: %v", part, err)
}
coordY, err := strconv.Atoi(coord[1])
if err != nil {
return fmt.Errorf("can't parse coordinate %s: %v", part, err)
}
if coordY > sm.MaxY {
sm.MaxY = coordY
}
if coordX < sm.MinX || sm.MinX == 0 {
sm.MinX = coordX
}
if coordX > sm.MaxX {
sm.MaxX = coordX
}
points = append(points, []int{coordX, coordY})
}
for i := 0; i < len(points)-1; i++ {
startX, startY := points[i][0], points[i][1]
endX, endY := points[i+1][0], points[i+1][1]
if startX == endX {
if startY > endY {
startY, endY = endY, startY
}
for y := startY; y <= endY; y++ {
sm.SetPoint(startX, y, Rock)
}
} else {
if startX > endX {
startX, endX = endX, startX
}
for x := startX; x <= endX; x++ {
sm.SetPoint(x, startY, Rock)
}
}
}
}
return nil
}
func AOC2022141Helper(input string) (int, error) {
sandMap := *&AOC202214SandMap{
Point: make(map[int]map[int]MapPoint),
Infinite: true,
}
err := sandMap.ParseRocks(input)
if err != nil {
return 0, fmt.Errorf("can't parse rocks: %v", err)
}
grains := 0
for {
if !sandMap.AddSand() {
break
}
grains += 1
}
return grains, nil
}
func AOC2022142Helper(input string) (int, error) {
sandMap := *&AOC202214SandMap{
Point: make(map[int]map[int]MapPoint),
}
err := sandMap.ParseRocks(input)
if err != nil {
return 0, fmt.Errorf("can't parse rocks: %v", err)
}
grains := 0
for {
grains += 1
if !sandMap.AddSand() {
break
}
}
return grains, nil
}
func AOC2022141(input string) (string, error) {
grains, err := AOC2022141Helper(input)
if err != nil {
return "", err
}
return fmt.Sprintf("%d", grains), nil
}
func AOC2022142(input string) (string, error) {
grains, err := AOC2022142Helper(input)
if err != nil {
return "", err
}
return fmt.Sprintf("%d", grains), nil
}