-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
211 lines (184 loc) · 3.39 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader, iterations int) int {
scanner := bufio.NewScanner(input)
options := make(map[string]string)
for scanner.Scan() {
line := scanner.Text()
addOptions(line, options)
}
grid := [][]bool{
{false, true, false},
{false, false, true},
{true, true, true},
}
for iteration := 0; iteration < iterations; iteration++ {
size := len(grid)
starting := 0
if size%2 == 0 {
starting = 2
} else if size%3 == 0 {
starting = 3
} else {
panic(size)
}
cells := len(grid) / starting
ngrid := make([][]bool, cells*(starting+1))
itrow := -1
for row := 0; row < len(grid); row += starting {
itrow++
for col := 0; col < len(grid[0]); col += starting {
r := -1
c := 0
s := ""
for j := 0; j < starting*starting; j++ {
if c == 0 {
r++
}
if grid[row+r][col+c] {
s += "#"
} else {
s += "."
}
c = (c + 1) % starting
}
if v, exists := options[s]; exists {
nrow := itrow*(starting+1) - 1
for i := 0; i < (starting+1)*(starting+1); i++ {
if i%(starting+1) == 0 {
nrow++
}
value := false
if v[i] == '#' {
value = true
}
ngrid[nrow] = append(ngrid[nrow], value)
}
} else {
panic(s)
}
}
}
grid = ngrid
//printGrid(grid)
}
sum := 0
for _, row := range grid {
for _, v := range row {
if v {
sum++
}
}
}
return sum
}
func printGrid(grid [][]bool) {
for _, row := range grid {
for _, v := range row {
if v {
fmt.Print("#")
} else {
fmt.Print(".")
}
}
fmt.Println()
}
fmt.Println()
}
func addOptions(s string, options map[string]string) {
del := lib.NewDelimiter(s, " ")
first := del.GetString(0)
second := del.GetString(2)
g1 := toGrid(first)
g2 := toGrid(second)
s2 := toString(g2)
options[toString(g1)] = s2
g1 = flip(g1)
options[toString(g1)] = s2
g1 = flip(g1)
g1 = rotate(g1)
options[toString(g1)] = s2
g1 = flip(g1)
options[toString(g1)] = s2
g1 = flip(g1)
g1 = rotate(g1)
options[toString(g1)] = s2
g1 = flip(g1)
options[toString(g1)] = s2
g1 = flip(g1)
g1 = rotate(g1)
options[toString(g1)] = s2
g1 = flip(g1)
options[toString(g1)] = s2
g1 = flip(g1)
}
func toGrid(s string) [][]bool {
var grid [][]bool
var cur []bool
for i := 0; i < len(s); i++ {
r := rune(s[i])
switch r {
case '#':
cur = append(cur, true)
case '.':
cur = append(cur, false)
case '/':
grid = append(grid, cur)
cur = make([]bool, 0)
}
}
grid = append(grid, cur)
return grid
}
func rotate(matrix [][]bool) [][]bool {
n := len(matrix)
m := len(matrix[0])
result := make([][]bool, m)
for i := range result {
result[i] = make([]bool, n)
}
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
result[j][n-1-i] = matrix[i][j]
}
}
return result
}
func flip(grid [][]bool) [][]bool {
for row := 0; row < len(grid); row++ {
l := 0
r := len(grid[row]) - 1
for l < r {
grid[row][l], grid[row][r] = grid[row][r], grid[row][l]
l++
r--
}
}
return grid
}
func toString(grid [][]bool) string {
s := ""
for _, row := range grid {
for _, v := range row {
if v {
s += "#"
} else {
s += "."
}
}
}
return s
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
_ = line
}
return 42
}