-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.go
214 lines (188 loc) · 4.43 KB
/
17.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"math"
"strings"
)
type int3 struct {
x, y, z int
}
type Cube struct {
location int3
on bool
nextOn bool
neighbors [27]*Cube
presentNeighbors int
}
var sceneMin = int3{math.MaxInt32, math.MaxInt32, math.MaxInt32}
var sceneMax = int3{-math.MaxInt32, -math.MaxInt32, -math.MaxInt32}
func (A int3) Add(B int3) int3 {
return int3{A.x + B.x, A.y + B.y, A.z + B.z}
}
func (A int3) Sub(B int3) int3 {
return int3{A.x - B.x, A.y - B.y, A.z - B.z}
}
func max(a int3, b int3) int3 {
var res int3 = b
if a.x > b.x {
res.x = a.x
}
if a.y > b.y {
res.y = a.y
}
if a.z > b.z {
res.z = a.z
}
return res
}
func min(a int3, b int3) int3 {
var res int3 = b
if a.x < b.x {
res.x = a.x
}
if a.y < b.y {
res.y = a.y
}
if a.z < b.z {
res.z = a.z
}
return res
}
func (c *Cube) setNeighbor(coord int3, neighbor *Cube) {
loc := (coord.z+1)*9 + (coord.y+1)*3 + coord.x + 1
c.neighbors[loc] = neighbor
}
func (c *Cube) getNeighbor(coord int3) *Cube {
loc := (coord.z+1)*9 + (coord.y+1)*3 + coord.x + 1
return c.neighbors[loc]
}
var allCubes []*Cube
var incomplete = make(map[int3]*Cube)
func addMissingNeighborsOff(cube *Cube) {
for x := -1; x <= 1; x++ {
for y := -1; y <= 1; y++ {
for z := -1; z <= 1; z++ {
if x == 0 && y == 0 && z == 0 {
continue
}
offset := int3{x, y, z}
if cube.getNeighbor(offset) == nil {
//fmt.Println("Going ", cube.location.x, cube.location.y, cube.location.z)
//fmt.Println("Going ", offset.x, offset.y, offset.z)
newCube := Cube{location: cube.location.Add(offset)}
addCube(&newCube)
//fmt.Println("\n")
}
}
}
}
}
func addCube(cube *Cube) {
//fmt.Println(cube.location.x, cube.location.y, cube.location.z)
for x := -1; x <= 1; x++ {
for y := -1; y <= 1; y++ {
for z := -1; z <= 1; z++ {
if x == 0 && y == 0 && z == 0 {
continue
}
offset := int3{x, y, z}
negOffset := int3{-x, -y, -z}
neighborCoord := cube.location.Add(offset)
if neighbor, ok := incomplete[neighborCoord]; ok {
cube.setNeighbor(offset, neighbor)
cube.presentNeighbors++
//fmt.Println("Doing ", neighborCoord.x, neighborCoord.y, neighborCoord.z)
neighbor.setNeighbor(negOffset, cube)
neighbor.presentNeighbors++
if neighbor.presentNeighbors == 26 {
delete(incomplete, neighborCoord)
}
}
}
}
}
if cube.presentNeighbors < 26 {
incomplete[cube.location] = cube
}
sceneMin = min(cube.location, sceneMin)
sceneMax = max(cube.location, sceneMax)
allCubes = append(allCubes, cube)
}
func main() {
var fileName string
var version2 bool
flag.StringVar(&fileName, "file", "data/in17.txt", "Input file to use")
flag.BoolVar(&version2, "v2", false, "Use task2 version")
flag.Parse()
content, _ := ioutil.ReadFile(fileName)
lines := strings.Split(string(content), "\n")
for y, line := range lines {
for x, c := range line {
if c == '#' {
cube := Cube{on: true, location: int3{x, y, 0}}
addCube(&cube)
}
}
}
for _, cube := range allCubes {
addMissingNeighborsOff(cube)
}
for i := 0; i < 6; i++ {
for c := 0; c < len(allCubes); c++ {
neighborsOn := 0
for _, neighbor := range allCubes[c].neighbors {
if neighbor != nil && neighbor.on {
neighborsOn++
}
}
if (neighborsOn == 3) || (allCubes[c].on && neighborsOn == 2) {
allCubes[c].nextOn = true
if _, ok := incomplete[allCubes[c].location]; ok {
addMissingNeighborsOff(allCubes[c])
}
} else {
allCubes[c].nextOn = false
}
}
for _, cube := range allCubes {
cube.on = cube.nextOn
}
}
// Output
sceneDim := sceneMax.Sub(sceneMin).Add(int3{1, 1, 1})
grid := make([]rune, sceneDim.x*sceneDim.y*sceneDim.z)
for z := 0; z < sceneDim.z; z++ {
for y := 0; y < sceneDim.y; y++ {
for x := 0; x < sceneDim.x; x++ {
pos := z*(sceneDim.x*sceneDim.y) + y*sceneDim.x + x
grid[pos] = '.'
}
}
}
for _, cube := range allCubes {
if cube.on {
off := cube.location.Sub(sceneMin)
pos := off.z*(sceneDim.x*sceneDim.y) + off.y*sceneDim.x + off.x
grid[pos] = '#'
}
}
for z := 1; z < sceneDim.z-1; z++ {
for y := 1; y < sceneDim.y-1; y++ {
for x := 1; x < sceneDim.x-1; x++ {
pos := z*(sceneDim.x*sceneDim.y) + y*sceneDim.x + x
fmt.Printf("%c", grid[pos])
}
fmt.Println("")
}
fmt.Println("")
}
onSum := 0
for _, c := range allCubes {
if c.on {
onSum++
}
}
fmt.Println(onSum)
}