-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.go
285 lines (266 loc) · 6.04 KB
/
day22.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
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
package main
import (
"fmt"
"github.com/dergeberl/aoc/utils"
"os"
"strconv"
"strings"
)
type region struct {
xMin, xMax int
yMin, yMax int
zMin, zMax int
state bool
}
type cubeRegions []region
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay22Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay22Part2(string(input)))
}
//SolveDay22Part1 returns number of on cubes within x=-50..50,y=-50..50,z=-50..50
func SolveDay22Part1(input string) int64 {
c := parseInput(input)
return c.countActive(countPart1)
}
//SolveDay22Part2 returns number of on cubes
func SolveDay22Part2(input string) int64 {
c := parseInput(input)
return c.countActive(countPart2)
}
//parseInput returns cubeRegions for a string input
func parseInput(input string) cubeRegions {
lines, _ := utils.InputToSlice(input)
c := make(cubeRegions, 0)
for l := range lines {
var state bool
var line string
if strings.HasPrefix(lines[l], "on") {
state = true
line = strings.TrimPrefix(lines[l], "on ")
} else {
line = strings.TrimPrefix(lines[l], "off ")
}
points := strings.Split(line, ",")
var xMin, xMax int
var yMin, yMax int
var zMin, zMax int
for i := range points {
tmpPoint := strings.Split(points[i], "=")
if len(tmpPoint) != 2 {
panic("wrong input")
}
tempRange := strings.Split(tmpPoint[1], "..")
if len(tempRange) != 2 {
panic("wrong input")
}
minA, _ := strconv.Atoi(tempRange[0])
maxA, _ := strconv.Atoi(tempRange[1])
switch tmpPoint[0] {
case "x":
xMin, xMax = minA, maxA
case "y":
yMin, yMax = minA, maxA
case "z":
zMin, zMax = minA, maxA
}
}
c = append(c, region{
xMin: xMin,
xMax: xMax,
yMin: yMin,
yMax: yMax,
zMin: zMin,
zMax: zMax,
state: state,
})
}
return c
}
//returns the number of active points after all rules, with a given count function
func (c cubeRegions) countActive(countFunc func(r region) int64) int64 {
if c == nil {
return 0
}
var regions cubeRegions
for i := range c {
var newRegions cubeRegions
for cur := range regions {
newRegions = append(newRegions, regions[cur].sub(c[i])...)
}
regions = newRegions
if c[i].state {
regions = append(regions, c[i])
}
}
var sum int64
for i := range regions {
sum += countFunc(regions[i])
}
return sum
}
//sub returns a new cubeRegions without the region which is subtracted
func (r region) sub(sRegion region) cubeRegions {
if sRegion.xMin > r.xMax ||
sRegion.yMin > r.yMax ||
sRegion.zMin > r.zMax ||
sRegion.xMax < r.xMin ||
sRegion.yMax < r.yMin ||
sRegion.zMax < r.zMin {
return cubeRegions{r}
}
if sRegion.xMin < r.xMin {
sRegion.xMin = r.xMin
}
if sRegion.xMax > r.xMax {
sRegion.xMax = r.xMax
}
if sRegion.yMin < r.yMin {
sRegion.yMin = r.yMin
}
if sRegion.yMax > r.yMax {
sRegion.yMax = r.yMax
}
if sRegion.zMin < r.zMin {
sRegion.zMin = r.zMin
}
if sRegion.zMax > r.zMax {
sRegion.zMax = r.zMax
}
newRegions := cubeRegions{r}
sRegion.xMax++
sRegion.yMax++
sRegion.zMax++
newRegions = newRegions.cutAll(&sRegion.xMin, nil, nil)
newRegions = newRegions.cutAll(&sRegion.xMax, nil, nil)
newRegions = newRegions.cutAll(nil, &sRegion.yMin, nil)
newRegions = newRegions.cutAll(nil, &sRegion.yMax, nil)
newRegions = newRegions.cutAll(nil, nil, &sRegion.zMin)
newRegions = newRegions.cutAll(nil, nil, &sRegion.zMax)
sRegion.xMax--
sRegion.yMax--
sRegion.zMax--
for i := range newRegions {
if newRegions[i].xMin == sRegion.xMin &&
newRegions[i].xMax == sRegion.xMax &&
newRegions[i].yMin == sRegion.yMin &&
newRegions[i].yMax == sRegion.yMax &&
newRegions[i].zMin == sRegion.zMin &&
newRegions[i].zMax == sRegion.zMax {
newRegions[i] = newRegions[len(newRegions)-1]
newRegions = newRegions[:len(newRegions)-1]
break
}
}
return newRegions
}
//cutAll cuts all cubeRegions at point and returns new cubeRegions
func (r cubeRegions) cutAll(x, y, z *int) cubeRegions {
var newRegions cubeRegions
for i := range r {
newRegions = append(newRegions, r[i].cut(x, y, z)...)
}
return newRegions
}
//cut one region at point and returns new cubeRegions
func (r region) cut(x, y, z *int) cubeRegions {
newRegions := make(cubeRegions, 0)
var tempRegion1 region
var tempRegion2 region
if x != nil {
tempRegion1 = region{
xMin: r.xMin,
xMax: *x - 1,
yMin: r.yMin,
yMax: r.yMax,
zMin: r.zMin,
zMax: r.zMax,
state: true,
}
tempRegion2 = region{
xMin: *x,
xMax: r.xMax,
yMin: r.yMin,
yMax: r.yMax,
zMin: r.zMin,
zMax: r.zMax,
state: true,
}
newRegions = append(newRegions, tempRegion1, tempRegion2)
}
if y != nil {
tempRegion1 = region{
xMin: r.xMin,
xMax: r.xMax,
yMin: r.yMin,
yMax: *y - 1,
zMin: r.zMin,
zMax: r.zMax,
state: true,
}
tempRegion2 = region{
xMin: r.xMin,
xMax: r.xMax,
yMin: *y,
yMax: r.yMax,
zMin: r.zMin,
zMax: r.zMax,
state: true,
}
newRegions = append(newRegions, tempRegion1, tempRegion2)
}
if z != nil {
tempRegion1 = region{
xMin: r.xMin,
xMax: r.xMax,
yMin: r.yMin,
yMax: r.yMax,
zMin: r.zMin,
zMax: *z - 1,
state: true,
}
tempRegion2 = region{
xMin: r.xMin,
xMax: r.xMax,
yMin: r.yMin,
yMax: r.yMax,
zMin: *z,
zMax: r.zMax,
state: true,
}
newRegions = append(newRegions, tempRegion1, tempRegion2)
}
if countPart2(tempRegion1) <= 0 || countPart2(tempRegion2) <= 0 {
return []region{r}
}
return newRegions
}
//countPart1 reruns number of blocks only if they are within 50 cubes away from 0
func countPart1(r region) int64 {
if r.xMax > 50 {
return 0
}
if r.yMax > 50 {
return 0
}
if r.zMax > 50 {
return 0
}
if r.xMin < -50 {
return 0
}
if r.yMin < -50 {
return 0
}
if r.zMin < -50 {
return 0
}
return int64((r.xMax - r.xMin + 1) * (r.yMax - r.yMin + 1) * (r.zMax - r.zMin + 1))
}
//countPart2 reruns number if blocks for a region
func countPart2(r region) int64 {
return int64((r.xMax - r.xMin + 1) * (r.yMax - r.yMin + 1) * (r.zMax - r.zMin + 1))
}