I2GPIFE63VQ723HFNXFYO3D4OOMR5QZFNQXTAR5TFOVMOHKXRHTAC
Tile 2647:
#....#####
.##......#
##......##
.....#..#.
.........#
.....#..##
#.#....#..
#......#.#
#....##..#
...##.....
Tile 1283:
######..#.
#.#..#.#..
..#..#...#
.#.##..#..
#......#..
#.#....##.
.#.....#.#
#.#..#.#.#
.#......##
...##.....
Tile 3547:
#.#.#.###.
#.........
#....##...
#.....#..#
#.....#.#.
##..##...#
#...##....
......#..#
#...##....
.....###.#
Tile 1451:
##..#.#...
#.#.......
##.#.....#
....#.....
...#...##.
......#.#.
#...##.##.
........#.
.#.##.#...
..##..#...
Tile 3137:
....#.##.#
#....#...#
..#.#.....
...####..#
.#.###...#
.......#..
##.##.#..#
.#.##....#
#...#....#
..##.##..#
Tile 2897:
###..#.##.
..#......#
.....#....
###.#....#
#.#..#...#
.#...##..#
##..##.##.
#.....#..#
.#......##
#.#.#.##.#
Tile 1093:
..#.#.#.#.
#.#.......
..##....#.
.#.....#.#
#........#
.#....#..#
##....#..#
#.##..#..#
..###...##
.######.##
Tile 1217:
#..#....##
#.....#...
##...##..#
#.....#...
..#.#..#..
#..#....##
.##.#.....
......#...
.#........
.#..###.#.
Tile 2801:
###..##.#.
.........#
##.#...###
#......#..
#........#
......#...
##.####...
.....##...
..#..#.##.
...###.##.
Tile 1361:
...#.##..#
....#.....
###.......
#......#..
.......##.
#...#..#..
#.....##.#
##........
#.#.......
###.#..###
Tile 2063:
...#....##
##...#..##
#........#
........##
#.......##
#.........
##.....##.
.....##..#
.#.##.#...
.#..#####.
Tile 3797:
##..#...#.
.###.#.##.
.....#.##.
..#.......
...#.#....
........##
#.#.#.##.#
#.....#.##
#.......#.
.....#.##.
Tile 1289:
####.##.#.
.....#....
#..#.#....
####...#..
#.#..#..#.
.#.##..#..
#........#
....#..#..
........#.
###.#.####
Tile 1427:
##.##..##.
###..#.##.
#..##...#.
#..#.#...#
#........#
#...##....
#........#
.....#..#.
.####....#
##.#.##.#.
Tile 1951:
....##.#.#
.........#
#........#
.#..#...#.
.....#####
#......#.#
...##....#
......#...
..#...#..#
....####.#
Tile 1483:
....####..
.......#.#
###..#..##
...#.#...#
#..##...##
##.#......
#...#..#..
..#...#.##
.........#
.#...#....
Tile 1789:
##..#####.
....#....#
........#.
..#.#..#.#
..##.#..##
.........#
.........#
#..#.#..##
....##....
#.#.......
Tile 2129:
#.###.#..#
....##...#
.#..#..##.
...###.##.
..#..#...#
....##...#
#.........
#...#..###
#...#.....
...#....##
Tile 2137:
..#.####.#
##...#.#..
.......###
.#.....#.#
.#....##.#
#.......#.
#....#...#
#.....####
......##.#
..#####.##
Tile 3761:
.####.#...
####..#..#
#...##..##
.#.....#.#
....#....#
#.......#.
...#..#..#
#.##...##.
...###...#
...##.#..#
Tile 1327:
..####.#.#
#..#......
......#.##
#..##.....
..##.##..#
#.#.#.....
####.....#
..#.......
#.#...##..
#.##....#.
Tile 2741:
.#..#...#.
#....#..#.
......##.#
....#.#..#
........##
...#..#...
......##..
#...#..#.#
......##..
..#..#..#.
Tile 1699:
.###..####
##.....#.#
.....##.##
#.#...##..
.#........
.#....#..#
#..#....#.
.#...#...#
#.......#.
##.#..#..#
Tile 1151:
..#.##....
##....#...
###.#..#.#
#.......##
....#.#..#
#...###...
.#..#.#..#
#.#..##..#
.#.#.#.#..
.###..####
Tile 2273:
#.#.#.#.##
..........
#......#..
#.....#...
#.#...#...
##....##..
##..##.#..
#.#####.##
##.##...##
#...##..##
Tile 1999:
##.##...##
#......#..
##..#.....
#........#
#.#...####
..#....#.#
#..#...#..
.........#
#...##....
##.##.##..
Tile 1721:
....##...#
###.#....#
.##..#....
.#.#.#....
...##....#
##..#....#
#....#.###
#.....##..
....#...##
..#.#.#..#
Tile 2521:
..#######.
#.#..##.#.
.#....##.#
..#...####
.......##.
##...###..
...##....#
.##.#.....
###..##..#
####.##.#.
Tile 2111:
..#.#..#..
...#.....#
..####...#
.#.#..##.#
.##..#.##.
........##
........##
#..#.#....
...#.###..
.#.#...#..
Tile 2767:
.#######..
##.......#
#...#.##..
....#...##
#........#
..#.###...
....#..#.#
##....#.##
..##....##
.#####.#..
Tile 2141:
####.#....
#..#.#...#
...#..#..#
.......#..
.....###.#
#....#....
.......#.#
.#...#..##
...#......
.###.####.
Tile 2557:
.#.##..#..
..##.....#
#.#.#....#
..##...#..
...#..##.#
..........
##......##
#..#......
#.#..#...#
##.#####..
Tile 2269:
.#.#...##.
#.......##
#.....##..
##.#......
#.##..###.
.#.....##.
....#....#
....#...##
#..##.....
#.#.#.#.##
Tile 3511:
.#.#.##...
.#.....##.
.#....#..#
#.#......#
#.#.#.....
#........#
..#.......
.##.#.....
##.#.....#
..####..##
Tile 2789:
#......#..
#...#.....
#.........
.......#.#
...#....##
#.##..###.
#...##...#
.........#
.........#
.###..##..
Tile 2971:
#.##.#....
...#.....#
.#....#...
#.#..##...
#.....#...
####.....#
#..###..##
#....#....
#..#.##...
#.#..###..
Tile 3719:
#.###.....
...#.....#
...##...##
.#..#.#..#
#..#.#..#.
#.#..#..##
#...###..#
.#.#..#.##
........#.
#....###..
Tile 1901:
.#...##.##
#.........
.#.#.....#
#.##.....#
#........#
#....#...#
.....##.##
##.###..##
....#....#
....##..##
Tile 3191:
#.#..###.#
#...#..##.
#.....#...
.#.#.#....
.#..##....
#.....#.#.
.##.......
....#....#
#..##.#...
####....##
Tile 3709:
..#......#
#..#...#.#
#.##....#.
.#..#.##..
..#......#
#....##...
##........
....#....#
.........#
.#.#..###.
Tile 1613:
...##..##.
#......#..
..##.#..##
......##..
.#..#..##.
.......##.
.......#.#
...#.#....
#......#.#
###..#....
Tile 2441:
..#.######
#.#.......
#..#.#....
....#...##
#...#...##
#.##...#.#
........##
#.#...#...
#..####.##
#.##.####.
Tile 1409:
..####.#.#
..##....#.
..#.#...#.
..##.##...
.#.##....#
#.....##.#
####.....#
###....#..
####..#.#.
#..##.##.#
Tile 1523:
.#.##..##.
#..#.#....
##.#.#...#
....#.##.#
#........#
#.#.......
#...##...#
...#..##.#
#.##...#..
.####..#..
Tile 1367:
#..#...#.#
#.#.......
..#..#....
.###..###.
###..#.##.
##...#..#.
#..#...#.#
......##..
##.....#.#
.#####..##
Tile 1783:
...#.####.
.####..#..
#....#.###
#.#..#.#.#
#.#.#.#..#
#.......##
#.##.#.#..
.#.#....#.
#..#.#...#
.###..##.#
Tile 1871:
.##..#.##.
#........#
#...#....#
##.#..##..
##.....##.
#.....#.##
........##
....#....#
#.........
....#.#..#
Tile 3217:
#.#...#.##
.........#
.........#
#...#.....
#....#.#.#
.........#
...#.##.##
#...#.....
.#..#....#
#..###.#.#
Tile 3163:
...##.#.##
#.#......#
....#...##
#.......##
###..#.#..
.#....####
##....#.##
#.......#.
.....#..#.
.##.#.#.##
Tile 3271:
##.#.#.##.
##....##.#
#.#.##..##
#.#...##.#
.##......#
#.....#.#.
#........#
##..##....
#.#..##..#
#..#.####.
Tile 2707:
..###.#...
#...#.....
#.#..#....
#..##...##
.###......
.#..##...#
#...#.....
....#.....
#..#.#....
.##....#.#
Tile 3083:
##..#.#.##
#..#....##
.........#
..#.#...##
..#.......
.#.#.....#
..#..#.#..
#...#.#..#
#..#.#....
#.###..##.
Tile 1051:
####...##.
...#.#...#
..........
..#.......
#......#..
.#.##.##..
#....#.#.#
#..#.#...#
#.#..##..#
......###.
Tile 3767:
.#..##.###
...#.#....
..#.....#.
#.#.......
.#.....#.#
##..#....#
#...#..#.#
........##
#........#
..#....##.
Tile 2267:
.#..#..#..
.#.#.#....
.#......#.
#...#....#
.###..#...
.##.#...##
..#.##.##.
...#.#.##.
##.#.##..#
.#.##.....
Tile 1973:
#.#####..#
.#.......#
#..#.#..#.
#.#.#.#.#.
.##.......
#.#.....#.
.#.......#
#...##.#.#
##.......#
.##...####
Tile 3671:
#..##.#.##
....##...#
.###.##...
.........#
#..#.....#
..##...#..
......#...
..#..#..##
..#.......
##..###..#
Tile 3221:
#.#..###.#
#..#....##
#..#......
#...#...##
..#..#..#.
#..##...#.
...#....#.
.....#..#.
##..#..#..
.....#...#
Tile 1549:
.###.##..#
#.#.##...#
#....#....
..........
#.#......#
##.#.#..##
...#.#..##
........#.
#.#....###
#....#...#
Tile 3461:
.######..#
#.......##
.......#..
.#...#....
..##....#.
#.....##..
##.#.#..#.
.........#
##.##.#...
....#...##
Tile 2459:
..##.##.#.
...#..#...
.........#
#.#..#..##
#.###.#...
##.#......
.......#..
.........#
........##
#.##...#..
Tile 3203:
.#...####.
..##..#.#.
#..#..##..
#.#....##.
...#.#....
.......###
#.....##..
....#....#
#......#..
###.......
Tile 2203:
#.#..##.##
.......#..
......#.##
#.......##
#..##.##.#
..#.....##
#.##.....#
#.#....#..
.##.....##
......#...
Tile 3637:
#...###.#.
#.........
..#.......
...#.....#
#..##....#
#........#
.......#..
#....#.#..
#.#..##..#
..#.#..##.
Tile 2467:
..##.##...
##....####
...#.#.#.#
#.##...#.#
...##.##..
#.....#...
##........
..#...#.#.
#...####.#
#......###
Tile 2411:
...##....#
...##..###
...##.####
#.#..##.#.
..##.#.###
.#..#.###.
....####.#
.....##.#.
#.........
.#..#..###
Tile 2221:
####.....#
#.#.....##
.#....#...
.#.#......
.##..#..#.
....#.....
.........#
##.......#
#....#....
.##.######
Tile 1487:
..#..##...
.........#
#..#...###
....#...#.
.#...##.#.
.....#.#.#
.....##...
#.##......
#.#.......
#.#####.#.
Tile 1481:
#.###.##..
....##...#
....#.....
...#......
##.###.#.#
#.##..####
..#......#
.#....##.#
..##.##.#.
.#####.#.#
Tile 1669:
#...##.##.
...#..#...
.##..#.#.#
#..#..#..#
#......#.#
.#......##
........#.
......#..#
.##..#.#.#
##.##....#
Tile 3167:
.#.####...
.........#
#......##.
.....#....
..#.#...##
#.#.####.#
...#....#.
.........#
#...#.#..#
#.#.#.#.#.
Tile 3347:
###...##..
#.#......#
...#.....#
..........
#.#.....#.
..####..##
..#.#.#..#
##...#..#.
..##.....#
#..#....#.
Tile 2213:
#..#####.#
..........
#..#.##.#.
...###.#.#
......##..
......#..#
.##.....##
..#....###
...####..#
.####.#.##
Tile 3329:
..##...#..
#.#....#.#
#...#..#..
......#.##
#...####.#
..........
##....##.#
#......##.
....##...#
..####.##.
Tile 3851:
#.#....##.
.........#
#.....#...
##.##.....
...#.###..
#....##...
.....#.##.
.#........
#......#.#
...#..#..#
Tile 2659:
#.#...#.#.
.....#.##.
#..##.####
#.#.##....
#....#..#.
...#...#..
...#....#.
#....#.#..
.##.#....#
.....#..#.
Tile 1933:
.####.##..
#..####...
.#..####..
.#.#.##...
......#.#.
##........
.#.#.....#
#..#......
....#.....
...#...##.
Tile 3299:
###.##..#.
.......#..
...#...##.
###...#.##
......##..
....#.#..#
.###......
.#.#####..
#..#.#..#.
.....#.#.#
Tile 3691:
...###...#
#.........
#.#.....##
#.#....#..
#..#...#..
..........
##...##..#
.#...#...#
#.....#.##
.###..#...
Tile 3733:
#..#.#####
.....#....
....###..#
#..#.#....
#.#..#.###
..###...##
......#.##
...###....
...#....#.
..##......
Tile 2131:
##.#..#.#.
.#...#..##
#.......#.
....##...#
.###..#...
...#####..
.....#...#
##..#..##.
..##....#.
.#...####.
Tile 1723:
.....#####
.#.#..#...
##......#.
#.......##
.###...#..
#..#......
#.........
......#..#
.........#
.###.##.##
Tile 3463:
##.#....##
#....##..#
..#.#.....
#.#...#..#
#....#....
..#....#.#
#...#..###
##....#.##
..#.#.....
.#..#.##..
Tile 2549:
#.####.#..
...##....#
##..#.##.#
..###.#..#
#.#......#
#........#
....#.....
#......#.#
#....####.
...##.#.##
Tile 1031:
#..#.#.#.#
......##..
#........#
.###......
..#..#..#.
##....##..
......#...
...#...###
.###...#..
.##.#.###.
Tile 1979:
#.######..
.#.#.....#
#........#
#..##.....
##........
##.....#..
......#...
.........#
.#........
..#.#####.
Tile 2939:
#.#...#.##
.#..#....#
.#.....#.#
##......##
...#..##..
#....#.##.
#...##.#.#
..#...#...
##.....#..
.....##.#.
Tile 2381:
..##.###.#
..##...#..
.#...#....
#......#.#
##.......#
#..####...
...#.#.#.#
#.##.....#
..#......#
#..#.##...
Tile 3943:
#.#.###..#
.......###
#.#...###.
#..##.#..#
#......#..
#.##...#.#
#.........
##....##.#
....#.#...
.###.#....
Tile 1553:
#####.####
#...#.....
#.#.....#.
##......#.
#....#.#..
.#.....#.#
##....#.#.
#........#
.........#
.#.....##.
Tile 2351:
.###.###..
#.....#...
##.##....#
..#..##.#.
#.#.......
#....#....
......##.#
##...##..#
.#.....#..
.#.###..#.
Tile 2311:
#.#.#..##.
#..###.#..
...##..#.#
###.......
##........
#.#.......
..##.....#
.#.####...
..#.#.#...
###..##.#.
Tile 1567:
..###.#.##
.#.....###
#...#..##.
#.......#.
.......#..
#....#....
...#.##.#.
....#...##
....#....#
#.#...##..
Tile 2579:
#.##..##..
#......#..
#..#..#..#
##.......#
....##.#.#
#.####..#.
#..#..#.##
#...#..#.#
...##...#.
#..#.###..
Tile 3593:
.#.##.#.##
#...#....#
..........
##....#..#
##......##
#.........
......#..#
...#.....#
....#....#
##..###..#
Tile 2281:
##....###.
...#......
#......#.#
##.#..#..#
###.#..##.
.#...#...#
..........
.#.###.#..
#..#......
#..#.##.#.
Tile 1193:
.......###
##..#..#..
.###...###
....#.###.
..#...#..#
#.#....#..
...####..#
#....#..##
.#.......#
.#.#...##.
Tile 3833:
...#####..
#..####...
#.#....###
...##.#.##
..#...#..#
.##.#####.
#..#..#..#
#...##....
.....#.#..
.##.##.#.#
Tile 2003:
.#.###.#..
.........#
..#..#....
#.........
#..##....#
.......#.#
......#...
#....##..#
.#......##
..#..##.#.
Tile 2731:
#.#..#..##
....#..#.#
..#...#...
..#..#....
#.#..#...#
#....##...
#.........
#..##..#.#
#.........
.###.#....
Tile 3881:
..##......
#...#..#.#
##...#....
....#.....
##.......#
.....#####
...#....##
.........#
..........
#..##.####
Tile 3673:
##..###.#.
...##....#
###.....##
#..#...#.#
#.##......
..#.#.....
..#.#....#
.###.....#
.###.##...
###.#..#.#
Tile 1021:
#..###.#..
###..##.#.
#..##....#
.....###..
....##...#
....#.....
#.##..#..#
..........
.......#.#
..#.##..#.
Tile 2423:
#.....####
.##.#....#
.#........
##.....#..
#.....###.
#...#...#.
#...#..#.#
.#..#..##.
##.......#
.#####.###
Tile 3923:
..#....###
#.....#..#
#...#.#.#.
.#.......#
#..#.#....
.......#.#
##....##.#
.#..#...#.
#...##..#.
..#.#.#..#
Tile 2753:
..####..#.
#.......#.
#.##.#..##
#.#.#.....
#..#......
....#.#...
.#.#..#..#
#.....#..#
##.#..#...
#####....#
Tile 3929:
....#####.
##..#.##..
##.#.#.##.
##...#.#..
#........#
.##.#..#..
#..#.##...
##..#...#.
.....#...#
###..####.
Tile 3041:
.##.#..#.#
#..#...#..
###..#..#.
.#.#....##
...##.....
#....#..##
#........#
##.#...#..
##....#..#
...#..#..#
Tile 3433:
..#.#.#...
#.#.......
.....#....
..#......#
#..#.....#
........##
##..##.##.
##........
#.#.##..##
###.###..#
Tile 2719:
..##..#..#
#.##..##..
#......#..
#...##..##
..#..#.#.#
#......###
..###..#..
....#.#..#
....##...#
##..#..###
Tile 1201:
.#...##.##
#........#
##...##...
..........
.....#.#..
#.##.....#
...#.##..#
.........#
.#.#.....#
.##...#...
Tile 1129:
...####..#
......##..
#.....##..
#.......#.
#......#..
...##....#
........##
##.#.#.#..
...#..##.#
...##....#
Tile 3019:
..#...###.
.....#.##.
#.##.....#
.#.##..#..
.#..###..#
..#.####.#
#..#.#...#
.......#.#
#..##.#..#
#.##....##
Tile 1747:
##.###.#..
#.......#.
#...#..#.#
##...##.#.
..###.#..#
#..#..##..
#...#.....
..#.......
...#..#.#.
.##..##.##
Tile 1741:
.##.#..#.#
#...##..##
#....#.#.#
##...##..#
##.......#
#...#..##.
...#.##.##
...#..#.#.
.......#.#
.#####.###
Tile 1867:
#..##.....
.......###
#..##....#
##...#....
...###....
##..#.....
.##.......
#.....###.
#...#..#.#
...###....
Tile 2803:
.#.##....#
#.####..#.
#.........
#.#......#
.......#.#
........#.
..#..#.#.#
....###...
#...##....
...###....
Tile 3643:
#..#..#.##
####.#..#.
#.#...#.##
.#..#.....
##....#..#
.##.......
.......#.#
...##.#...
.....#.##.
#...####.#
Tile 2437:
..###..###
....#.....
..........
#.#..#.###
##...####.
....##....
...##.....
##..#.##..
#......#..
#.#.....#.
Tile 1069:
..####....
##..##...#
.#..#..##.
.#....##.#
###.#.#.##
...##..#.#
##....#...
#.#....#.#
.#.....#.#
#.#.#.....
Tile 1381:
.###.#.##.
....#..#..
#.......##
#...#.....
.#...#..##
...#....##
#..#.###..
..######.#
#....#...#
#######.#.
Tile 2617:
..##..#.#.
#.....##.#
..#.#..#..
.##.#..#..
###...#.#.
.###.##...
#.#.......
#..##.#..#
##.....#..
.##..#..##
Tile 2393:
.##..#.#.#
..#.#..###
..##..#.##
....#.....
#...#.....
##.#.....#
.#.#..#.#.
##.....#..
.......#.#
####..#...
Tile 3529:
#.#...##.#
......#..#
.........#
#.....#...
.......#..
.....#.#.#
.....#....
#....#.#.#
....#.##.#
.####.#..#
Tile 2953:
...##...#.
##.#.#..##
#...#.....
##.#...###
...#......
#.#.#..#.#
.#...#...#
##....#.##
.......#..
.#.#..#...
Tile 3617:
#..##...##
......#...
#....#....
..........
.######.##
##..#.#.##
#.#...#...
........#.
.######.##
##...###.#
Tile 3863:
.##.#...##
#...#.....
..#.#....#
#....#..##
.....###..
#.#......#
#.......#.
...#.....#
#.........
..###....#
Tile 3727:
#.###.##.#
..........
...##.....
..#..#..##
#......###
#....##...
###.##....
.....#....
##.####.#.
#..#.#.###
Tile 3803:
###..#.##.
.##......#
.........#
###.....##
....###..#
.......#.#
........##
#..#......
##......##
#.###..#..
Tile 1579:
#...##.###
.....#.###
.##...#...
#.#..#..#.
..##.....#
.........#
..........
#.....#.##
.....#....
.###..#...
Tile 1049:
#..#.##.##
##......##
..#.##...#
#.......#.
###.....#.
.....#.#.#
...#......
..##......
#.#....#..
##..#.#...
Tile 2687:
##..#.##..
.#........
##..#...#.
.#.#.....#
.#..#.#..#
#.###..#..
..#......#
#.......##
#..#.....#
#.##.#..##
Tile 1637:
#..##...##
##..#....#
...#....#.
#....#....
.....#...#
#...#...##
.#....#...
#.........
..#....#..
.#.####...
Tile 3527:
.#....#.#.
#.......#.
..#....#.#
####.#.#.#
...#..#...
###..#.###
##..#....#
#.##....##
..#......#
.....#.#..
Tile 2963:
#.#.#.#.#.
#.....#...
##.#.....#
..##......
..#.......
.#...#.##.
###......#
##....#..#
.#...#..##
..##..##.#
Tile 2287:
##.######.
.#.##.##..
#..#....##
##.#.#...#
.......##.
#...##...#
...##..#..
##....#.#.
....#.##..
..#.#..###
Tile 3677:
###.....##
#..#.#..#.
#.#.......
.....#..##
..........
......#.##
.....#..#.
#..#...#..
.##......#
#...##.##.
Tile 3559:
..#..#.##.
###......#
..#.##....
#.#..#....
##..##..##
..#...#.#.
#.....#.##
....#....#
...#.#...#
...#.###..
Tile 2837:
..#...#...
.....##...
#.#..#...#
....#....#
...####.##
#.........
...#...##.
.#..###.#.
....#.....
.###.##.#.
Tile 3539:
..##....#.
........#.
......#..#
...#..#...
###....###
#...#.....
.#........
#.....#...
..##.#..#.
..###..#.#
Tile 1667:
.#..####..
.....#....
......#...
#.#...##.#
#...#.#..#
##.#.#...#
##..#..#..
#...##...#
.#..###...
..#..####.
Tile 2791:
#.##.###.#
...#..#...
##.....###
...#.#..##
.........#
.###...#..
...#.....#
##.....##.
###.......
#..#.#....
Tile 2609:
..##.#....
##.#.#...#
#.#..#....
#.........
...#..#..#
#...#.#...
##.##....#
.###......
##.....##.
#.#...#.#.
Tile 3061:
####..#.##
#.....##..
..........
......#...
..#.#..###
.#.#..#..#
.#...#...#
#........#
.....#.#..
#..#....##
const std = @import("std");
const PATH = "input/day20.txt";
const Str = []const u8;
const TileData = [10][10]bool;
const Tile = struct {
id: u16,
data: TileData,
// rotating left
fn rotate(self: *@This()) void {
var rotated: TileData = undefined;
for (self.data) |line, row| {
for (line) |item, col| {
rotated[self.data.len - 1 - col][row] = item;
}
}
self.data = rotated;
}
fn flip(self: *@This(), axis: u1) void {
var flipped: TileData = undefined;
switch (axis) {
0 => return,
1 => {
// horizontal flip
for (self.data) |slice, row| {
flipped[self.data.len - 1 - row] = slice;
}
},
}
self.data = flipped;
}
};
const Image = struct {
tiles: []Tile,
size: u8 = undefined,
visited: std.AutoHashMap(u16, void),
current_tile: [2]u8 = .{ 0, 0 },
order: std.ArrayList(usize),
fn checkLeft(self: @This(), candidate: Tile) bool {
// left column is always good
if (self.current_tile[1] == 0) return true;
// last item's right side should be equal to candidate left side
for (candidate.data) |_, row| {
if (self.tiles[self.order.items[self.order.items.len - 1]]
.data[row][candidate.data.len - 1] != candidate.data[row][0]) return false;
}
return true;
}
fn checkUpper(self: @This(), candidate: Tile) bool {
// top row is always good
if (self.current_tile[0] == 0) return true;
// index of the upper element in self.order
const upper = (self.current_tile[0] - 1) * self.size + self.current_tile[1];
// compare upper last line with candidate's first line
return std.mem.eql(
bool,
&self.tiles[self.order.items[upper]].data[candidate.data.len - 1],
&candidate.data[0],
);
}
fn solve(self: *@This()) bool {
// all item in place
if (self.current_tile[0] == self.size) {
return true;
}
var idx: usize = 0;
while (idx < self.tiles.len) : (idx += 1) {
const candidate = self.tiles[idx];
if (self.visited.contains(candidate.id)) {
// next 8 item has the same id, skip them!
// while adds one more, so it is 7 here
idx += 7;
continue;
}
if (!self.checkUpper(candidate)) continue;
if (!self.checkLeft(candidate)) continue;
self.order.append(idx) catch unreachable;
self.visited.put(candidate.id, {}) catch unreachable;
// move current_tile forward
const old_tile = self.current_tile;
if (self.current_tile[1] != self.size - 1) {
self.current_tile[1] += 1;
} else {
self.current_tile[0] += 1;
self.current_tile[1] = 0;
}
if (self.solve()) {
return true;
} else {
// restore items
_ = self.order.pop();
_ = self.visited.remove(candidate.id);
self.current_tile = old_tile;
}
}
return false;
}
};
pub fn first(allocator: ?std.mem.Allocator) anyerror!usize {
var image = Image{
.tiles = undefined,
.visited = std.AutoHashMap(u16, void).init(allocator.?),
.order = std.ArrayList(usize).init(allocator.?),
};
image.tiles = try parseInput(allocator.?, @embedFile(PATH));
defer {
allocator.?.free(image.tiles);
image.visited.deinit();
image.order.deinit();
}
image.size = @intCast(u8, std.math.sqrt(image.tiles.len / 8));
if (!image.solve()) unreachable;
var mul: usize = 1;
for ([_]usize{
0,
image.size - 1,
image.size * image.size - image.size,
image.size * image.size - 1,
}) |order_no| {
mul *= image.tiles[image.order.items[order_no]].id;
}
return mul;
}
pub fn second(allocator: ?std.mem.Allocator) anyerror!usize {
_ = allocator;
return 0;
}
fn parseInput(allocator: std.mem.Allocator, input: Str) anyerror![]Tile {
var ret = std.ArrayList(Tile).init(allocator);
var lines = std.mem.split(u8, input, "\n");
var t: Tile = undefined;
var l_no: usize = 0;
while (lines.next()) |line| {
if (line.len == 0) {
try flipRotate(&ret, &t);
l_no = 0;
t = undefined;
continue;
}
switch (line[0]) {
'T' => {
t.id = try std.fmt.parseUnsigned(u16, line[5..9], 10);
},
'.', '#' => {
for (line) |ch, idx| {
switch (ch) {
'.' => t.data[l_no][idx] = false,
'#' => t.data[l_no][idx] = true,
else => unreachable,
}
}
l_no += 1;
},
else => unreachable,
}
}
// add the last item...
try flipRotate(&ret, &t);
return ret.toOwnedSlice();
}
fn flipRotate(ret: *std.ArrayList(Tile), t: *Tile) !void {
for ([_]u1{ 0, 1 }) |flip| {
t.flip(flip);
for ([_]u2{ 0, 1, 2, 3 }) |_| {
t.rotate();
try ret.append(t.*);
}
}
}
test "day20a" {
try std.testing.expectEqual(@as(usize, 83775126454273), try first(std.testing.allocator));
}
test "day20b" {
try std.testing.expectEqual(@as(usize, 0), try second(std.testing.allocator));
}
const test_input =
\\Tile 2311:
\\..##.#..#.
\\##..#.....
\\#...##..#.
\\####.#...#
\\##.##.###.
\\##...#.###
\\.#.#.#..##
\\..#....#..
\\###...#.#.
\\..###..###
\\
\\Tile 1951:
\\#.##...##.
\\#.####...#
\\.....#..##
\\#...######
\\.##.#....#
\\.###.#####
\\###.##.##.
\\.###....#.
\\..#.#..#.#
\\#...##.#..
\\
\\Tile 1171:
\\####...##.
\\#..##.#..#
\\##.#..#.#.
\\.###.####.
\\..###.####
\\.##....##.
\\.#...####.
\\#.##.####.
\\####..#...
\\.....##...
\\
\\Tile 1427:
\\###.##.#..
\\.#..#.##..
\\.#.##.#..#
\\#.#.#.##.#
\\....#...##
\\...##..##.
\\...#.#####
\\.#.####.#.
\\..#..###.#
\\..##.#..#.
\\
\\Tile 1489:
\\##.#.#....
\\..##...#..
\\.##..##...
\\..#...#...
\\#####...#.
\\#..#.#.#.#
\\...#.#.#..
\\##.#...##.
\\..##.##.##
\\###.##.#..
\\
\\Tile 2473:
\\#....####.
\\#..#.##...
\\#.##..#...
\\######.#.#
\\.#...#.#.#
\\.#########
\\.###.#..#.
\\########.#
\\##...##.#.
\\..###.#.#.
\\
\\Tile 2971:
\\..#.#....#
\\#...###...
\\#.#.###...
\\##.##..#..
\\.#####..##
\\.#..####.#
\\#..#.#..#.
\\..####.###
\\..#.#.###.
\\...#.#.#.#
\\
\\Tile 2729:
\\...#.#.#.#
\\####.#....
\\..#.#.....
\\....#..#.#
\\.##..##.#.
\\.#.####...
\\####.#.#..
\\##.####...
\\##..#.##..
\\#.##...##.
\\
\\Tile 3079:
\\#.#.#####.
\\.#..######
\\..#.......
\\######....
\\####.#..#.
\\.#...#.##.
\\#.#####.##
\\..#.###...
\\..#.......
\\..#.###...
;