-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17b.go
More file actions
120 lines (99 loc) · 3.27 KB
/
day17b.go
File metadata and controls
120 lines (99 loc) · 3.27 KB
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
package main
import (
"fmt"
)
type cubeCoord4D struct {
xCoord int
yCoord int
zCoord int
wCoord int
}
func countActiveCubes4D(cubeMap map[cubeCoord4D]bool) int {
var activeCubes int = 0
for _, active := range cubeMap {
if active {
activeCubes++
}
}
return activeCubes
}
func setInitialState4D(puzzleInput []string) (cubeMap map[cubeCoord4D]bool) {
var tmpCoords cubeCoord4D
cubeMap = make(map[cubeCoord4D]bool)
for y := 0; y < len(puzzleInput); y++ {
for x := 0; x < len(puzzleInput[0]); x++ {
if puzzleInput[y][x] == '#' {
tmpCoords = cubeCoord4D{xCoord: x, yCoord: y, zCoord: 0, wCoord: 0}
cubeMap[tmpCoords] = true
}
}
}
return cubeMap
}
func checkCubeNeighbours4D(checkCoords cubeCoord4D, cubeMap map[cubeCoord4D]bool) int {
var activeNeighbours int = 0
for z := checkCoords.zCoord - 1; z <= checkCoords.zCoord+1; z++ {
for y := checkCoords.yCoord - 1; y <= checkCoords.yCoord+1; y++ {
for x := checkCoords.xCoord - 1; x <= checkCoords.xCoord+1; x++ {
for w := checkCoords.wCoord - 1; w <= checkCoords.wCoord+1; w++ {
if x == checkCoords.xCoord && y == checkCoords.yCoord && z == checkCoords.zCoord && w == checkCoords.wCoord {
// This is our coords to check so dont count this one!
} else {
if cubeMap[cubeCoord4D{xCoord: x, yCoord: y, zCoord: z, wCoord: w}] {
activeNeighbours++
}
}
}
}
}
}
return activeNeighbours
}
func applyCycleRules4D(cubeMap map[cubeCoord4D]bool, debug bool) (newCubeMap map[cubeCoord4D]bool) {
var activeNeighbours int
newCubeMap = make(map[cubeCoord4D]bool)
// Loop through old array and apply rules when copying to new array
for checkCoords := range cubeMap {
// We need to check all the neighbours of the stored cube
for z := checkCoords.zCoord - 1; z <= checkCoords.zCoord+1; z++ {
for y := checkCoords.yCoord - 1; y <= checkCoords.yCoord+1; y++ {
for x := checkCoords.xCoord - 1; x <= checkCoords.xCoord+1; x++ {
for w := checkCoords.wCoord - 1; w <= checkCoords.wCoord+1; w++ {
activeNeighbours = checkCubeNeighbours4D(cubeCoord4D{xCoord: x, yCoord: y, zCoord: z, wCoord: w}, cubeMap)
if cubeMap[cubeCoord4D{xCoord: x, yCoord: y, zCoord: z, wCoord: w}] {
// cube is active. Stays active if has 2 or 3 active neighbours, otherwise goes inactive
if activeNeighbours == 2 || activeNeighbours == 3 {
newCubeMap[cubeCoord4D{xCoord: x, yCoord: y, zCoord: z, wCoord: w}] = true
} else {
newCubeMap[cubeCoord4D{xCoord: x, yCoord: y, zCoord: z, wCoord: w}] = false
}
} else {
// cube is inactive. Becomes active if exactly 3 of its neighbours are active
if activeNeighbours == 3 {
newCubeMap[cubeCoord4D{xCoord: x, yCoord: y, zCoord: z, wCoord: w}] = true
}
}
}
}
}
}
}
if debug {
fmt.Println("----------")
fmt.Println(newCubeMap)
}
return newCubeMap
}
func howManyActiveCubesPartB(filename string, part byte, debug bool) int {
var cubeMap map[cubeCoord4D]bool
puzzleInput, _ := readFile(filename)
cubeMap = setInitialState4D(puzzleInput)
if debug {
fmt.Println("--- Initial State ---")
fmt.Println(cubeMap)
}
for bootCycle := 0; bootCycle < 6; bootCycle++ {
cubeMap = applyCycleRules4D(cubeMap, debug)
}
return countActiveCubes4D(cubeMap)
}