-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.go
240 lines (201 loc) · 6.03 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
package main
import (
"fmt"
"strings"
"github.com/Alex-Waring/AoC/utils"
)
type Location struct {
x int
y int
z int
}
type Brick struct {
index int
front Location
back Location
}
// Return if there is a block above
func block_below(grid map[Location]int, bricks map[int]Brick, brick_index int) (bool, int) {
brick := bricks[brick_index]
// if z is 1, we are already on the bottom
if min(brick.front.z, brick.back.z) == 1 {
return true, 0
}
for x := brick.front.x; x <= brick.back.x; x++ {
for y := brick.front.y; y <= brick.back.y; y++ {
z := min(brick.front.z, brick.back.z)
below := Location{x: x, y: y, z: z - 1}
if block, exists := grid[below]; exists {
return true, block
}
}
}
return false, 0
}
// Return a set of all of the blocks below a block
func bricks_below(grid map[Location]int, bricks map[int]Brick, brick_index int) map[int]bool {
return_bricks := make(map[int]bool)
brick := bricks[brick_index]
for x := brick.front.x; x <= brick.back.x; x++ {
for y := brick.front.y; y <= brick.back.y; y++ {
z := min(brick.front.z, brick.back.z)
below := Location{x: x, z: z - 1, y: y}
if below_index, exists := grid[below]; exists {
return_bricks[below_index] = true
}
}
}
return return_bricks
}
// return a set of all of the blocks above a block
func bricks_above(grid map[Location]int, bricks map[int]Brick, brick_index int) map[int]bool {
return_bricks := make(map[int]bool)
brick := bricks[brick_index]
for x := brick.front.x; x <= brick.back.x; x++ {
for y := brick.front.y; y <= brick.back.y; y++ {
z := max(brick.front.z, brick.back.z)
above := Location{x: x, z: z + 1, y: y}
if above_index, exists := grid[above]; exists {
return_bricks[above_index] = true
}
}
}
return return_bricks
}
func copy_grid(grid map[Location]int) map[Location]int {
return_grid := map[Location]int{}
for loc, index := range grid {
return_grid[loc] = index
}
return return_grid
}
func copy_bricks(bricks map[int]Brick) map[int]Brick {
return_bricks := map[int]Brick{}
for loc, index := range bricks {
return_bricks[loc] = index
}
return return_bricks
}
func main() {
input := utils.ReadInput("input.txt")
grid := map[Location]int{}
bricks := map[int]Brick{}
max_x := 0
max_y := 0
max_z := 0
for index, line := range input {
front := strings.Split(line, "~")[0]
back := strings.Split(line, "~")[1]
front_x, front_y, front_z := strings.Split(front, ",")[0], strings.Split(front, ",")[1], strings.Split(front, ",")[2]
back_x, back_y, back_z := strings.Split(back, ",")[0], strings.Split(back, ",")[1], strings.Split(back, ",")[2]
brick := Brick{
index: index,
front: Location{x: utils.IntegerOf(front_x), y: utils.IntegerOf(front_y), z: utils.IntegerOf(front_z)},
back: Location{x: utils.IntegerOf(back_x), y: utils.IntegerOf(back_y), z: utils.IntegerOf(back_z)},
}
for x := brick.front.x; x <= brick.back.x; x++ {
for y := brick.front.y; y <= brick.back.y; y++ {
for z := brick.front.z; z <= brick.back.z; z++ {
loc := Location{x: x, y: y, z: z}
grid[loc] = index
max_x = max(max_x, x)
max_y = max(max_y, y)
max_z = max(max_z, z)
}
}
}
bricks[index] = brick
}
// Loop through the bricks, keeping track if any have falled, and settle them to the bottom
rotation := false
for !rotation {
rotation = true
for index, brick := range bricks {
is_block_below, _ := block_below(grid, bricks, index)
if !is_block_below {
rotation = false
for x := brick.front.x; x <= brick.back.x; x++ {
for y := brick.front.y; y <= brick.back.y; y++ {
for z := brick.front.z; z <= brick.front.z; z++ {
loc := Location{x: x, y: y, z: z}
delete(grid, loc)
loc.z = loc.z - 1
grid[loc] = index
new_brick := Brick{
index: index,
front: Location{x: brick.front.x, y: brick.front.y, z: brick.front.z - 1},
back: Location{x: brick.back.x, y: brick.back.y, z: brick.back.z - 1},
}
bricks[index] = new_brick
}
}
}
}
}
}
// For every brick, if there is a brick above it check if that brick has multiple bricks below it
// If every brick above a brick has multiple supporting it, it can be disintegrated
disintegrated := make(map[int]bool)
for index, _ := range bricks {
bricks_above := bricks_above(grid, bricks, index)
can_disintegrate := true
for brick := range bricks_above {
bricks_below := bricks_below(grid, bricks, brick)
if len(bricks_below) <= 1 {
can_disintegrate = false
}
}
if can_disintegrate {
disintegrated[index] = true
}
}
fmt.Println(len(disintegrated))
// For part 2, brute force. For every brick dissintegrate it and loop through every brick, counting
// the number of times we fall
total := 0
for index, brick := range bricks {
grid_copy := copy_grid(grid)
bricks_copy := copy_bricks(bricks)
delete(bricks_copy, index)
// Disintegrate the brick
for x := brick.front.x; x <= brick.back.x; x++ {
for y := brick.front.y; y <= brick.back.y; y++ {
for z := brick.front.z; z <= brick.front.z; z++ {
loc := Location{x: x, y: y, z: z}
delete(grid_copy, loc)
}
}
}
bricks_fallen := 0
// See what's fallen
rotation := false
for !rotation {
rotation = true
for index, brick := range bricks_copy {
is_block_below, _ := block_below(grid_copy, bricks_copy, index)
if !is_block_below {
rotation = false
for x := brick.front.x; x <= brick.back.x; x++ {
for y := brick.front.y; y <= brick.back.y; y++ {
for z := brick.front.z; z <= brick.front.z; z++ {
loc := Location{x: x, y: y, z: z}
delete(grid_copy, loc)
loc.z = loc.z - 1
grid_copy[loc] = index
new_brick := Brick{
index: index,
front: Location{x: brick.front.x, y: brick.front.y, z: brick.front.z - 1},
back: Location{x: brick.back.x, y: brick.back.y, z: brick.back.z - 1},
}
bricks_copy[index] = new_brick
bricks_fallen++
}
}
}
}
}
}
total += bricks_fallen
}
fmt.Println(total)
}