-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
187 lines (158 loc) · 4.83 KB
/
grid.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
package d18
import (
"fmt"
"strings"
"github.com/gammazero/deque"
"github.com/simonski/goutils"
)
type Grid struct {
data map[string]*Cube
}
func (g *Grid) Bounds() (int, int, int, int, int, int) {
max_x := 0
max_y := 0
max_z := 0
min_x := 100
min_y := 100
min_z := 100
for _, c := range g.data {
max_x = goutils.Max(max_x, c.x)
max_y = goutils.Max(max_y, c.y)
max_z = goutils.Max(max_z, c.z)
min_x = goutils.Min(min_x, c.x)
min_y = goutils.Min(min_y, c.y)
min_z = goutils.Min(min_z, c.z)
}
return max_x, max_y, max_z, min_x, min_y, min_z
}
func (g *Grid) Add(c *Cube) {
g.data[c.Key()] = c
}
func (g *Grid) Size() int {
return len(g.data)
}
func NewGrid(input string) *Grid {
g := Grid{data: make(map[string]*Cube)}
lines := strings.Split(input, "\n")
for _, line := range lines {
c := NewCube(line, true)
g.Add(c)
}
return &g
}
func (g *Grid) Get(x int, y int, z int) *Cube {
key := Key(x, y, z)
return g.data[key]
}
func (g *Grid) GetOrEmpty(x int, y int, z int) *Cube {
key := Key(x, y, z)
result := g.data[key]
if result == nil {
c := &Cube{x: x, y: y, z: z, solid: false}
g.data[key] = c
return c
}
return result
}
// func (g *Grid) Contains(x int, y int, z int) bool {
// return g.Get(x, y, z) != nil
// }
// returns all neighbouring coordinates that can be directly connected
func (g *Grid) NeighboursConnectable(x int, y int, z int) []*Point3D {
results := make([]*Point3D, 0)
results = append(results, NewPoint3D(x-1, y, z))
results = append(results, NewPoint3D(x+1, y, z))
results = append(results, NewPoint3D(x, y-1, z))
results = append(results, NewPoint3D(x, y+1, z))
results = append(results, NewPoint3D(x, y, z-1))
results = append(results, NewPoint3D(x, y, z+1))
return results
}
func (g *Grid) CubesConnected(x int, y int, z int) []*Cube {
results := make([]*Cube, 0)
for _, p := range g.NeighboursConnectable(x, y, z) {
c := g.GetOrEmpty(p.x, p.y, p.z)
if c.solid {
results = append(results, c)
}
}
return results
}
// returns number of connected and open sizes
func (g *Grid) CountSides() (int, int) {
total_connected := 0
total_not_connected := 0
for _, c := range g.data {
connected := len(g.CubesConnected(c.x, c.y, c.z))
total_connected += connected
total_not_connected += (6 - connected)
}
return total_connected, total_not_connected
}
func (g *Grid) get_external_surface_area() (int, map[*Cube]bool) {
// """ Determine surface area of all cubes that can reach the outside. """
cubes_to_outside := make(map[*Cube]bool) // # cache cubes we have already identified a path to outside for
no_path_to_outside := make(map[*Cube]bool) // set() # store all internal empty
surfaces_to_outside := 0
// # Loop through the cubes and find any that can reach outside
for _, cube := range g.data {
for _, p := range g.NeighboursConnectable(cube.x, cube.y, cube.z) {
neighbour := g.GetOrEmpty(p.x, p.y, p.z)
if g._has_path_to_outside(neighbour, cubes_to_outside, no_path_to_outside) {
cubes_to_outside[neighbour] = true
surfaces_to_outside += 1
} else {
no_path_to_outside[neighbour] = true
}
}
}
return surfaces_to_outside, no_path_to_outside
}
func (g *Grid) _has_path_to_outside(cube *Cube, cubes_to_outside map[*Cube]bool, no_path_to_outside map[*Cube]bool) bool {
/*
Perform BFS to flood fill from this empty cube.
Param cubes_to_outside is to cache cubes we've seen before, that we know have a path.
Param internal_cubues is to cache cubes we've seen before, that are internal. """
*/
frontier := deque.New[*Cube]()
frontier.PushFront(cube)
explored := make(map[*Cube]bool)
explored[cube] = true
max_x, max_y, max_z, min_x, min_y, min_z := g.Bounds()
for {
if frontier.Len() == 0 {
break
}
if frontier.Len()%50000 == 0 {
fmt.Printf("Q size is %v\n", frontier.Len())
}
current_cube := frontier.PopFront() // # FIFO for BFS
// # Check caches
if cubes_to_outside[current_cube] {
return true //# We've got out from here before
}
if no_path_to_outside[current_cube] {
continue // # This cube doesn't have a path, so no point checking its neighbours
}
if g.Get(current_cube.x, current_cube.y, current_cube.z).solid { // } in self.filled_cubes:
// if filled_cubes[current_cube] { // } in self.filled_cubes:
continue // # This path is blocked
}
// # Check if we've followed a path outside of the bounds
if current_cube.x > max_x || current_cube.y > max_y || current_cube.z > max_z {
return true
}
if current_cube.x < min_x || current_cube.y < min_y || current_cube.z < min_z {
return true
}
// # We want to look at all neighbours of this empty space
for _, p := range g.NeighboursConnectable(current_cube.x, current_cube.y, current_cube.z) {
neighbour := g.GetOrEmpty(p.x, p.y, p.z)
if !explored[neighbour] {
frontier.PushBack(neighbour)
explored[neighbour] = true
}
}
}
return false
}