-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.go
182 lines (170 loc) · 3.94 KB
/
day17.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
i, err := ioutil.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
input := string(i)
fmt.Printf("Part 1: %v\n", SolveDay17Part1(stringListToSlice(input)))
fmt.Printf("Part 2: %v\n", SolveDay17Part2(stringListToSlice(input)))
}
type coordinates struct {
x int
y int
z int
w int
}
//SolveDay17Part1 returns the number of active cubes after the six-cycle boot process (with 3 dimensional space)
func SolveDay17Part1(input []string) (s int) {
runs := 6
startX := len(input)
startY := len(input[0])
curState := make(map[coordinates]bool)
for y, line := range input {
for x, state := range line {
if string(state) == "#" {
curState[coordinates{x: x, y: y, z: 0, w: 0}] = true
}
}
}
newState := make(map[coordinates]bool)
for key, value := range curState {
newState[key] = value
}
for i := 1; i <= runs; i++ {
for iZ := 0 - i; iZ <= i; iZ++ {
for iY := 0 - i; iY < startY+i; iY++ {
for iX := 0 - i; iX < startX+i; iX++ {
if curState[coordinates{x: iX, y: iY, z: iZ, w: 0}] {
// enabled
count := countNeighbors(curState, iZ, iY, iX)
if count != 2 && count != 3 {
newState[coordinates{x: iX, y: iY, z: iZ}] = false
}
} else {
// disabled
count := countNeighbors(curState, iZ, iY, iX)
if count == 3 {
newState[coordinates{x: iX, y: iY, z: iZ, w: 0}] = true
}
}
}
}
}
for key, value := range newState {
curState[key] = value
}
}
//count active
for _, state := range curState {
if state {
s++
}
}
return
}
//SolveDay17Part2 returns the number of active cubes after the six-cycle boot process (with 4 dimensional space)
func SolveDay17Part2(input []string) (s int) {
runs := 6
startX := len(input)
startY := len(input[0])
curState := make(map[coordinates]bool)
for y, line := range input {
for x, state := range line {
if string(state) == "#" {
curState[coordinates{x: x, y: y, z: 0, w: 0}] = true
}
}
}
newState := make(map[coordinates]bool)
for key, value := range curState {
newState[key] = value
}
for i := 1; i <= runs; i++ {
for iZ := 0 - i; iZ <= i; iZ++ {
for iW := 0 - i; iW <= i; iW++ {
for iY := 0 - i; iY < startY+i; iY++ {
for iX := 0 - i; iX < startX+i; iX++ {
if curState[coordinates{x: iX, y: iY, z: iZ, w: iW}] {
// enabled
count := countNeighbors(curState, iZ, iY, iX, iW)
if count != 2 && count != 3 {
newState[coordinates{x: iX, y: iY, z: iZ, w: iW}] = false
}
} else {
// disabled
count := countNeighbors(curState, iZ, iY, iX, iW)
if count == 3 {
newState[coordinates{x: iX, y: iY, z: iZ, w: iW}] = true
}
}
}
}
}
}
for key, value := range newState {
curState[key] = value
}
}
//count active
for _, state := range curState {
if state {
s++
}
}
return
}
//countNeighbors returns the number of active neighbor cubes (maximum 4)
func countNeighbors(curState map[coordinates]bool, nums ...int) (count int) {
var z, y, x, w int
check4d := false
if len(nums) == 3 {
z = nums[0]
y = nums[1]
x = nums[2]
w = 0
} else if len(nums) == 4 {
z = nums[0]
y = nums[1]
x = nums[2]
w = nums[3]
check4d = true
} else {
return 0
}
for iz := z - 1; iz <= z+1; iz++ {
for iw := w - 1; iw <= w+1; iw++ {
if iw != 0 && !check4d {
continue
}
for iy := y - 1; iy <= y+1; iy++ {
for ix := x - 1; ix <= x+1; ix++ {
if iz == z && iy == y && ix == x && iw == w {
continue
}
if curState[coordinates{x: ix, y: iy, z: iz, w: iw}] {
count++
if count >= 4 {
return
}
}
}
}
}
}
return
}
//Helper functions
//stringListToSlice converts the list of strings (each string one row) to a slice
func stringListToSlice(list string) (s []string) {
for _, line := range strings.Split(strings.TrimSuffix(list, "\n"), "\n") {
s = append(s, line)
}
return
}