-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20-2.go
250 lines (235 loc) · 6.15 KB
/
20-2.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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"math"
"math/bits"
"strings"
)
var grid [][]int
var pieces = make(map[int]Piece)
var border2Tile = make(map[uint]map[int]bool)
type Piece struct {
dim int
content [][]byte
border [4]uint
numUnmatched int
}
func reverseBorder(border uint, dim int) uint {
return bits.Reverse(border) >> (64 - dim)
}
func border2Hash(hash uint, dim int) uint {
testHash := reverseBorder(hash, dim)
if testHash < hash {
hash = testHash
}
return hash
}
func rotateTile(dim int, content [][]byte) [][]byte {
newContent := make([][]byte, len(content))
for y := 0; y < dim; y++ {
newContent[y] = make([]byte, len(content[y]))
for x := 0; x < dim; x++ {
newContent[y][x] = content[x][dim-1-y]
}
}
return newContent
}
func flipTileV(dim int, content [][]byte) [][]byte {
newContent := make([][]byte, len(content))
for y := 0; y < dim; y++ {
newContent[y] = make([]byte, len(content[y]))
for x := 0; x < dim; x++ {
newContent[y][x] = content[dim-1-y][x]
}
}
return newContent
}
func flipTileH(dim int, content [][]byte) [][]byte {
newContent := make([][]byte, len(content))
for y := 0; y < dim; y++ {
newContent[y] = make([]byte, len(content[y]))
for x := 0; x < dim; x++ {
newContent[y][x] = content[y][dim-1-x]
}
}
return newContent
}
func flipPieceH(p int) {
piece := pieces[p]
piece.content = flipTileH(piece.dim, piece.content)
piece.border[1], piece.border[3] = piece.border[3], piece.border[1]
for i := 0; i < 4; i++ {
piece.border[i] = reverseBorder(piece.border[i], piece.dim)
}
pieces[p] = piece
}
func flipPieceV(p int) {
piece := pieces[p]
piece.content = flipTileV(piece.dim, piece.content)
piece.border[0], piece.border[2] = piece.border[2], piece.border[0]
for i := 0; i < 4; i++ {
piece.border[i] = reverseBorder(piece.border[i], piece.dim)
}
pieces[p] = piece
}
func rotatePiece(p int) {
piece := pieces[p]
piece.content = rotateTile(piece.dim, piece.content)
piece.border[0], piece.border[1], piece.border[2], piece.border[3] = piece.border[1], piece.border[2], piece.border[3], piece.border[0]
pieces[p] = piece
}
func prepareRefPiece() int {
refPiece := -1
for _, ids := range border2Tile {
if len(ids) == 1 {
for id := range ids {
p := pieces[id]
p.numUnmatched++
if p.numUnmatched == 2 {
refPiece = id
break
}
pieces[id] = p
}
}
}
for len(border2Tile[border2Hash(pieces[refPiece].border[1], pieces[refPiece].dim)]) < 2 ||
len(border2Tile[border2Hash(pieces[refPiece].border[2], pieces[refPiece].dim)]) < 2 {
rotatePiece(refPiece)
}
return refPiece
}
func fillGrid(id int, x, y int) {
if grid[y][x] != 0 {
return
}
grid[y][x] = id
piece := pieces[id]
// We start with tile in top left, search matches on right and bottom
for c := 1; c <= 2; c++ {
opposite := (c + 2) % 4
code := piece.border[c]
hash := border2Hash(code, piece.dim)
// Find tile matching border code, transform to fit
if len(border2Tile[hash]) > 1 {
var other int
for entry := range border2Tile[hash] {
if entry != id {
other = entry
}
}
for border2Hash(pieces[other].border[opposite], pieces[other].dim) != hash {
rotatePiece(other)
}
if pieces[other].border[opposite] == code {
if c == 1 {
flipPieceV(other)
} else {
flipPieceH(other)
}
}
if c == 1 {
fillGrid(other, x+1, y)
} else {
fillGrid(other, x, y+1)
}
}
}
}
func countWaves(pieceDim int, gridDim int) int {
imgDim := gridDim * (pieceDim - 2)
outImg := make([][]byte, imgDim)
for y := 0; y < gridDim; y++ {
for ty := 1; ty < pieceDim-1; ty++ {
outImg[y*(pieceDim-2)+ty-1] = make([]byte, imgDim)
for x := 0; x < gridDim; x++ {
for tx := 1; tx < pieceDim-1; tx++ {
outImg[y*(pieceDim-2)+ty-1][x*(pieceDim-2)+tx-1] = pieces[grid[y][x]].content[ty][tx]
}
}
}
}
// Monster to look for
monsterText := []string{
" # ",
"# ## ## ###",
" # # # # # # "}
// Iterate over image and count occurrences
numMonsters := 0
for cfg := 0; cfg < 8 && numMonsters == 0; cfg++ {
checkImg := outImg
if cfg > 4 {
checkImg = flipTileH(imgDim, checkImg)
}
for y := 0; y < imgDim-len(monsterText); y++ {
NewMonster:
for x := 0; x < imgDim-len(monsterText[0]); x++ {
for my := 0; my < len(monsterText); my++ {
for mx := 0; mx < len(monsterText[0]); mx++ {
if monsterText[my][mx] == '#' && checkImg[y+my][x+mx] != '#' {
continue NewMonster
}
}
}
numMonsters++
}
}
outImg = rotateTile(imgDim, outImg)
}
wavesInImage, wavesInMonster := 0, 0
for y := 0; y < imgDim; y++ {
wavesInImage += strings.Count(string(outImg[y]), "#")
}
for y := 0; y < len(monsterText); y++ {
wavesInMonster += strings.Count(monsterText[y], "#")
}
return wavesInImage - wavesInMonster*numMonsters
}
func main() {
var fileName string
flag.StringVar(&fileName, "file", "data/in20.txt", "Input file to use")
flag.Parse()
content, _ := ioutil.ReadFile(fileName)
tiles := bytes.Split(content, []byte("\n\n"))
// Create grid
gridDim := int(math.Sqrt(float64(len(tiles))))
grid = make([][]int, gridDim)
for g := 0; g < gridDim; g++ {
grid[g] = make([]int, gridDim)
}
// Scan tiles and compute border codes
var id int
for _, tile := range tiles {
lines := bytes.Split(tile, []byte("\n"))
fmt.Fscanf(bytes.NewReader(lines[0]), "Tile %d:", &id)
piece := Piece{dim: len(lines[1]), content: lines[1:]}
// Compute binary border codes (counter clock-wise)
x, y, dx, dy := 0, 0, 1, 0
for i := 0; i < 4; i++ {
for r := 0; r < piece.dim; r++ {
if piece.content[y][x] == '#' {
piece.border[i] |= (1 << r)
}
x, y = x+dx, y+dy
}
x, y, dx, dy = x-dx, y-dy, -dy, dx
}
pieces[id] = piece
}
// Convert border code to transform invariant hash and find matches
for id, p := range pieces {
for _, border := range p.border {
hash := border2Hash(border, p.dim)
if border2Tile[hash] == nil {
border2Tile[hash] = make(map[int]bool)
}
border2Tile[hash][id] = true
}
}
// Find starting piece, fill grid and count waves and monsters
fillGrid(prepareRefPiece(), 0, 0)
fmt.Println(countWaves(pieces[id].dim, gridDim))
}