-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
570 lines (442 loc) · 11.7 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package main
import (
"bufio"
"fmt"
"io"
"math"
"os"
"path"
"sort"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "20", "input.txt"))
if err != nil {
panic(err)
}
return input
}
const (
edgeTop = 0
edgeRight = 1
edgeBottom = 2
edgeLeft = 3
)
func getEdge(t [][]byte, which int) []byte {
if t == nil {
return nil
}
switch which {
case edgeTop:
// top
return t[0]
case edgeRight:
// right
newContainer := make([]byte, len(t))
for i := 0; i < len(t); i++ {
newContainer[i] = t[i][len(t[i])-1]
}
return newContainer
case edgeBottom:
// bottom
return t[len(t)-1]
case edgeLeft:
// left
newContainer := make([]byte, len(t))
for i := 0; i < len(t); i++ {
newContainer[i] = t[i][0]
}
return newContainer
default:
panic(which)
}
}
func match(edgeA, edgeB []byte) bool {
if edgeB == nil {
return true
}
if len(edgeA) != len(edgeB) {
return false
}
for i := 0; i < len(edgeA); i++ {
if edgeA[i] != edgeB[i] {
return false
}
}
return true
}
func rotate(tile [][]byte) [][]byte {
// assumes tile is a square
newTile := make([][]byte, len(tile))
for x := 0; x < len(tile); x++ {
newTile[x] = make([]byte, len(tile[x]))
}
for x := 0; x < len(tile); x++ {
for y := 0; y < len(tile); y++ {
newTile[len(tile)-y-1][x] = tile[x][y]
}
}
return newTile
}
func flipY(tile [][]byte) [][]byte {
newTile := make([][]byte, len(tile))
for x := 0; x < len(tile); x++ {
newTile[x] = make([]byte, len(tile[x]))
}
for x := 0; x < len(tile); x++ {
for y := 0; y < len(tile); y++ {
newTile[x][y] = tile[len(tile)-x-1][y]
}
}
return newTile
}
func flipX(tile [][]byte) [][]byte {
newTile := make([][]byte, len(tile))
for x := 0; x < len(tile); x++ {
newTile[x] = make([]byte, len(tile[x]))
}
for x := 0; x < len(tile); x++ {
for y := 0; y < len(tile); y++ {
newTile[x][y] = tile[x][len(tile[x])-y-1]
}
}
return newTile
}
func eq(tileA, tileB [][]byte) bool {
for x := 0; x < len(tileA); x++ {
for y := 0; y < len(tileB); y++ {
if tileA[x][y] != tileB[x][y] {
return false
}
}
}
return true
}
func contains(tiles [][][]byte, tile [][]byte) bool {
for _, tileA := range tiles {
if eq(tileA, tile) {
return true
}
}
return false
}
func generateRotatesFlips(tile [][]byte) [][][]byte {
var retTiles [][][]byte
for rotation := 0; rotation < 4; rotation += 1 {
for xFlip := 0; xFlip < 2; xFlip += 1 {
for yFlip := 0; yFlip < 2; yFlip += 1 {
newTile := tile
for r := 0; r < rotation; r += 1 {
newTile = rotate(tile)
}
if xFlip == 1 {
newTile = flipY(newTile)
}
if yFlip == 1 {
newTile = flipX(newTile)
}
if !contains(retTiles, newTile) {
retTiles = append(retTiles, newTile)
}
}
}
}
return retTiles
}
type assignment struct {
id int
tile [][]byte
}
func (a assignment) getEdge(which int) []byte {
return getEdge(a.tile, which)
}
func possibilities(tiles map[int][][]byte, used map[int]bool, assignments [][]*assignment, x, y int) []*assignment {
var topMatch []byte
var rightMatch []byte
var bottomMatch []byte
var leftMatch []byte
if y != 0 && assignments[x][y-1] != nil {
topMatch = assignments[x][y-1].getEdge(edgeBottom)
}
if x != len(assignments)-1 && assignments[x+1][y] != nil {
rightMatch = assignments[x+1][y].getEdge(edgeLeft)
}
if y != len(assignments[x])-1 && assignments[x][y+1] != nil {
bottomMatch = assignments[x][y+1].getEdge(edgeTop)
}
if x != 0 && assignments[x-1][y] != nil {
leftMatch = assignments[x-1][y].getEdge(edgeRight)
}
var possib []*assignment
tileOrder := make([]int, len(tiles))
i := 0
for id := range tiles {
tileOrder[i] = id
i += 1
}
sort.Ints(tileOrder)
for _, id := range tileOrder {
tile := tiles[id]
if used[id] {
continue
}
ways := generateRotatesFlips(tile)
for _, way := range ways {
if match(getEdge(way, edgeTop), topMatch) &&
match(getEdge(way, edgeRight), rightMatch) &&
match(getEdge(way, edgeBottom), bottomMatch) &&
match(getEdge(way, edgeLeft), leftMatch) {
possib = append(possib, &assignment{
id: id,
tile: way,
})
}
}
}
return possib
}
func assign(tiles map[int][][]byte, used map[int]bool, assignments [][]*assignment) bool {
for x := 0; x < len(assignments); x++ {
for y := 0; y < len(assignments[x]); y++ {
if assignments[x][y] != nil {
continue
}
for _, possibAssignment := range possibilities(tiles, used, assignments, x, y) {
assignments[x][y] = possibAssignment
used[possibAssignment.id] = true
if assign(tiles, used, assignments) {
return true
}
assignments[x][y] = nil
delete(used, possibAssignment.id)
}
return false
}
}
return true
}
func solve(r io.Reader) {
tiles := make(map[int][][]byte)
scanner := bufio.NewScanner(r)
var currTile [][]byte
var currId int
for scanner.Scan() {
row := scanner.Text()
if strings.Index(row, "Tile ") == 0 {
var err error
currId, err = strconv.Atoi(row[5 : len(row)-1])
if err != nil {
panic(err)
}
} else if len(row) != 0 {
currTile = append(currTile, []byte(row))
} else {
if currId == 0 || currTile == nil {
panic("bad format")
}
tiles[currId] = currTile
currTile = nil
currId = 0
}
}
if scanner.Err() != nil {
panic(scanner.Err())
}
if currTile != nil {
tiles[currId] = currTile
}
//fmt.Println(tiles)
size := int(math.Sqrt(float64(len(tiles))))
assignments := make([][]*assignment, size)
for i := 0; i < size; i++ {
assignments[i] = make([]*assignment, size)
}
used := make(map[int]bool)
if !assign(tiles, used, assignments) {
panic("impossible")
}
ret := assignments[0][0].id * assignments[0][size-1].id * assignments[size-1][0].id * assignments[size-1][size-1].id
fmt.Println(assignments[0][0].id, assignments[0][size-1].id, assignments[size-1][0].id, assignments[size-1][size-1].id, ret)
/*for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
fmt.Print(assignments[x][y].id, " ")
}
fmt.Println()
}*/
newAssignments := make([][]*assignment, size)
for i := 0; i < size; i++ {
newAssignments[i] = make([]*assignment, size)
}
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
newAssignments[size-x-1][size-y-1] = assignments[y][x]
}
}
//fmt.Println()
//ret = assignments[0][0].id * assignments[0][size - 1].id * assignments[size - 1][0].id * assignments[size - 1][size - 1].id
//fmt.Println(assignments[0][0].id, assignments[0][size - 1].id, assignments[size - 1][0].id, assignments[size - 1][size - 1].id, ret)
//fmt.Println("----------------")
//printTile(assignments[0][0].tile)
//picture := amalgamate(assignments)
//printTile(picture)
picture := amalgamate(newAssignments)
printTile(picture)
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
fmt.Print(newAssignments[x][y].id, " ")
}
fmt.Println()
}
monster := loadVeryScaryMonster()
removed := findAndRemove(picture, monster)
printTile(removed)
fmt.Println(count(removed, '#'))
}
func count(tile [][]byte, b byte) int {
var sum int
for x := 0; x < len(tile); x++ {
for y := 0; y < len(tile[x]); y++ {
if tile[x][y] == b {
sum += 1
}
}
}
return sum
}
func find(haystack [][]byte, needle [][]byte) (bool, int, int) {
for x := 0; x < len(haystack)-len(needle); x++ {
for y := 0; y < len(haystack[x])-len(needle[x%len(needle)]); y++ {
all := true
for i := 0; i < len(needle); i++ {
for j := 0; j < len(needle[i]); j++ {
if needle[i][j] == 0 {
continue
}
if haystack[x+i][y+j] != needle[i][j] {
all = false
break
}
}
if !all {
break
}
}
if all {
return true, x, y
}
}
}
return false, 0, 0
}
func remove(haystack [][]byte, needle [][]byte, x, y int) {
for i := 0; i < len(needle); i++ {
for j := 0; j < len(needle[i]); j++ {
if needle[i][j] == 0 {
continue
}
haystack[x+i][y+j] = 'O'
}
}
}
func findAndRemove(haystack [][]byte, needle [][]byte) [][]byte {
for {
ways := generateRotatesFlips(haystack)
var didFind bool
for _, way := range ways {
found, x, y := find(way, needle)
if found {
remove(way, needle, x, y)
haystack = way
didFind = true
break
}
}
if !didFind {
break
}
}
return haystack
}
func loadVeryScaryMonster() [][]byte {
const raw = " # \n# ## ## ###\n # # # # # # "
parts := strings.Split(raw, "\n")
monster := make([][]byte, len(parts))
for y := 0; y < len(parts); y++ {
monster[y] = make([]byte, len(parts[y]))
}
for y := 0; y < len(parts); y++ {
for x := 0; x < len(parts[y]); x++ {
if parts[y][x] == '#' {
monster[y][x] = '#'
}
}
}
return monster
}
func amalgamate(assignments [][]*assignment) [][]byte {
// 1, 0 for biggy testing and -2, 1 for real
extraRoom := -2
borderSize := 1
// assume square
targetTileSize := len(assignments[0][0].tile) + extraRoom
size := targetTileSize * len(assignments)
ret := make([][]byte, size)
for i := 0; i < size; i++ {
ret[i] = make([]byte, size)
}
for x := 0; x < len(assignments); x++ {
for y := 0; y < len(assignments[x]); y++ {
tile := assignments[x][y].tile
targetX := targetTileSize * x //(len(assignments) - x - 1)
targetY := targetTileSize * y //(len(assignments[x]) - y - 1)
tile = flipX(rotate(tile))
for sourceX := borderSize; sourceX < len(tile)-borderSize; sourceX++ {
for sourceY := borderSize; sourceY < len(tile[sourceX])-borderSize; sourceY++ {
ret[targetX+sourceX-borderSize][targetY+sourceY-borderSize] = tile[sourceY][sourceX]
}
}
/*for x := 0; x < targetTileSize; x++ {
for y :=0; y < targetTileSize; y++ {
actX := targetTileSize - x - 1 + targetX
actY := targetTileSize - y - 1 + targetY
regX := x + targetX
regY := y + targetY
//fmt.Println("--", ret[x][y], ret[y][x])
tmp := ret[actX][actY]
ret[actX][actY] = ret[regY][regX]
ret[regY][regX] = tmp
//fmt.Println(ret[x][y], ret[y][x], "--")
}
}*/
//printTile(ret)
}
}
return ret
}
func printTile(tile [][]byte) {
for x := 0; x < len(tile); x++ {
for y := 0; y < len(tile[x]); y++ {
if tile[x][y] == 0 {
fmt.Print(" ")
continue
}
fmt.Printf("%s", []byte{tile[x][y]})
}
fmt.Println()
}
fmt.Println()
}
func main() {
test := [][]byte{
{'A', 'B', 'C'},
{'D', 'E', 'F'},
{'G', 'H', 'I'}}
printTile(test)
/*printTile(rotate(test))
printTile(flipY(test))
printTile(flipX(test))*/
//printTile(loadVeryScaryMonster())
solve(strings.NewReader("Tile 2311:\n..##.#..#.\n##..#.....\n#...##..#.\n####.#...#\n##.##.###.\n##...#.###\n.#.#.#..##\n..#....#..\n###...#.#.\n..###..###\n\nTile 1951:\n#.##...##.\n#.####...#\n.....#..##\n#...######\n.##.#....#\n.###.#####\n###.##.##.\n.###....#.\n..#.#..#.#\n#...##.#..\n\nTile 1171:\n####...##.\n#..##.#..#\n##.#..#.#.\n.###.####.\n..###.####\n.##....##.\n.#...####.\n#.##.####.\n####..#...\n.....##...\n\nTile 1427:\n###.##.#..\n.#..#.##..\n.#.##.#..#\n#.#.#.##.#\n....#...##\n...##..##.\n...#.#####\n.#.####.#.\n..#..###.#\n..##.#..#.\n\nTile 1489:\n##.#.#....\n..##...#..\n.##..##...\n..#...#...\n#####...#.\n#..#.#.#.#\n...#.#.#..\n##.#...##.\n..##.##.##\n###.##.#..\n\nTile 2473:\n#....####.\n#..#.##...\n#.##..#...\n######.#.#\n.#...#.#.#\n.#########\n.###.#..#.\n########.#\n##...##.#.\n..###.#.#.\n\nTile 2971:\n..#.#....#\n#...###...\n#.#.###...\n##.##..#..\n.#####..##\n.#..####.#\n#..#.#..#.\n..####.###\n..#.#.###.\n...#.#.#.#\n\nTile 2729:\n...#.#.#.#\n####.#....\n..#.#.....\n....#..#.#\n.##..##.#.\n.#.####...\n####.#.#..\n##.####...\n##..#.##..\n#.##...##.\n\nTile 3079:\n#.#.#####.\n.#..######\n..#.......\n######....\n####.#..#.\n.#...#.##.\n#.#####.##\n..#.###...\n..#.......\n..#.###..."))
solve(input())
}