-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwenty-one.go
258 lines (228 loc) · 6.04 KB
/
twenty-one.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"bufio"
"fmt"
"math"
"os"
"strings"
)
func main() {
rules := createRules()
part := 2
m := [][]rune{
{'.', '#', '.'},
{'.', '.', '#'},
{'#', '#', '#'}}
if part == 1 {
m = enhance(m, rules, 5)
fmt.Println("Part 1 : ", howManyOn(m))
} else {
m = enhance(m, rules, 18)
fmt.Println("Part 2 : ", howManyOn(m))
}
}
func enhance(matrix [][]rune, rules map[string]string, times int) [][]rune {
m := matrix
for range times {
result := [][][]rune{}
if len(m)%2 == 0 {
// Array of 2D arrays
twod := extract2x2Matrices(m, len(m))
for _, square := range twod {
// Resulting matrices from the transformation.
result = append(result, applyTransformation(square, rules))
}
// The 2by2 arrays get transformed into multiple 3x3
m = merge3x3Matrices(result)
} else {
treed := extract3x3Matrices(m, len(m))
for _, square := range treed {
result = append(result, applyTransformation(square, rules))
}
// The 3by3 arrays get transformed into multiple 4x4
m = merge4x4Matrices(result)
}
}
return m
}
// Function to merge 4x4 matrices into a larger matrix
func merge4x4Matrices(matrices [][][]rune) [][]rune {
numMatrices := len(matrices)
dim := int(math.Sqrt(float64(numMatrices))) * 4
largeMatrix := make([][]rune, dim)
for i := range largeMatrix {
largeMatrix[i] = make([]rune, dim)
}
// Fill the larger matrix with the 4x4 matrices
for idx, matrix := range matrices {
rowOffset := (idx / (dim / 4)) * 4
colOffset := (idx % (dim / 4)) * 4
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
largeMatrix[rowOffset+i][colOffset+j] = matrix[i][j]
}
}
}
return largeMatrix
}
func merge3x3Matrices(matrices [][][]rune) [][]rune {
// Calculate the dimension of the larger matrix
numMatrices := len(matrices)
dim := int(math.Sqrt(float64(numMatrices))) * 3
largeMatrix := make([][]rune, dim)
for i := range largeMatrix {
largeMatrix[i] = make([]rune, dim)
}
// Fill the larger matrix with the 3x3 matrices
for idx, matrix := range matrices {
rowOffset := (idx / (dim / 3)) * 3
colOffset := (idx % (dim / 3)) * 3
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
largeMatrix[rowOffset+i][colOffset+j] = matrix[i][j]
}
}
}
return largeMatrix
}
func applyTransformation(matrix [][]rune, rules map[string]string) [][]rune {
// Get the string representation of this matrix
str := MatrixToStr(matrix)
transformation := rules[str]
return StrToMatrix(transformation)
}
func extract2x2Matrices(matrix [][]rune, N int) [][][]rune {
var result [][][]rune
for i := 0; i < N; i += 2 {
for j := 0; j < N; j += 2 {
// Create a new 2x2 matrix
newMatrix := [][]rune{
{matrix[i][j], matrix[i][j+1]},
{matrix[i+1][j], matrix[i+1][j+1]},
}
result = append(result, newMatrix)
}
}
return result
}
func extract3x3Matrices(matrix [][]rune, N int) [][][]rune {
var result [][][]rune
for i := 0; i < N; i += 3 {
for j := 0; j < N; j += 3 {
// Create a new 3x3 matrix
newMatrix := [][]rune{
{matrix[i][j], matrix[i][j+1], matrix[i][j+2]},
{matrix[i+1][j], matrix[i+1][j+1], matrix[i+1][j+2]},
{matrix[i+2][j], matrix[i+2][j+1], matrix[i+2][j+2]},
}
result = append(result, newMatrix)
}
}
return result
}
func createRules() map[string]string {
rules := map[string]string{}
f, _ := os.Open("input.txt")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.Split(scanner.Text(), "=>")
// Don't forget to trim space
equivRules := createRotations(strings.TrimSpace(line[0]))
// For every equivalent rotation, associate it with the transformation.
for _, rule := range equivRules {
rules[rule] = strings.TrimSpace(line[1])
}
}
return rules
}
func createRotations(rule string) []string {
// Add the original rule
rotations := []string{rule}
// Create the matrix from the rule
matrix := StrToMatrix(rule)
// Apply the all transformations, and return to string format and append.
rotate90 := rotateClockwise(matrix)
rotations = append(rotations, MatrixToStr(rotate90))
rotate180 := rotateClockwise(rotate90)
rotations = append(rotations, MatrixToStr(rotate180))
rotate270 := rotateClockwise(rotate180)
rotations = append(rotations, MatrixToStr(rotate270))
// Flip and apply the rotations on the flipped version.
flipH := flipHorizontal(matrix)
rotations = append(rotations, MatrixToStr(flipH))
flipRot90 := rotateClockwise(flipH)
rotations = append(rotations, MatrixToStr(flipRot90))
flipRot180 := rotateClockwise(flipRot90)
rotations = append(rotations, MatrixToStr(flipRot180))
flipRot270 := rotateClockwise(flipRot180)
rotations = append(rotations, MatrixToStr(flipRot270))
return rotations
}
func StrToMatrix(rule string) [][]rune {
matrix := [][]rune{}
rows := strings.Split(rule, "/")
for _, row := range rows {
r := []rune{}
for _, char := range row {
r = append(r, char)
}
matrix = append(matrix, r)
}
return matrix
}
func MatrixToStr(matrix [][]rune) string {
s := strings.Builder{}
for i, row := range matrix {
for _, char := range row {
s.WriteRune(char)
}
if i != len(matrix)-1 {
s.WriteRune('/')
}
}
return s.String()
}
// Rotate but create a new matrix in the process.
func rotateClockwise(matrix [][]rune) [][]rune {
// Create new matrix
m := make([][]rune, len(matrix))
for i := range len(matrix) {
m[i] = make([]rune, len(matrix[i]))
copy(m[i], matrix[i])
}
// reverse the matrix
for i, j := 0, len(matrix)-1; i < j; i, j = i+1, j-1 {
m[i], m[j] = m[j], m[i]
}
// transpose it
for i := 0; i < len(matrix); i++ {
for j := 0; j < i; j++ {
m[i][j], m[j][i] = m[j][i], m[i][j]
}
}
return m
}
func flipHorizontal(matrix [][]rune) [][]rune {
// Create new matrix
m := make([][]rune, len(matrix))
for i := range len(matrix) {
m[i] = make([]rune, len(matrix[i]))
copy(m[i], matrix[i])
}
// reverse the matrix
for i, j := 0, len(matrix)-1; i < j; i, j = i+1, j-1 {
m[i], m[j] = matrix[j], matrix[i]
}
return m
}
func howManyOn(m [][]rune) int {
s := 0
for _, row := range m {
for _, char := range row {
if char == '#' {
s++
}
}
}
return s
}