|
| 1 | +from collections import namedtuple |
1 | 2 | from itertools import product, permutations
|
2 | 3 |
|
3 | 4 | import numpy as np
|
4 | 5 | from imgaug.augmenters.flip import fliplr, flipud
|
5 | 6 | from imgaug.augmenters.geometric import Rot90
|
6 | 7 | from tqdm import tqdm
|
7 | 8 |
|
| 9 | +Tile = namedtuple("Tile", "idx ns ts e") |
| 10 | + |
8 | 11 | with open("input2.txt") as f:
|
9 | 12 | parts = f.read().split("\n\n")
|
10 | 13 |
|
@@ -38,24 +41,150 @@ def edge_has_fit(tiles, tile_idx, edge):
|
38 | 41 | t = transform(tile, *trans)
|
39 | 42 | for f in [t[0], t[:,-1], t[-1], t[:,0]]:
|
40 | 43 | if np.array_equal(f, e):
|
41 | | - return idx |
42 | | - return None |
| 44 | + return idx, trans |
| 45 | + return None, None |
43 | 46 |
|
44 | 47 | res = []
|
45 | 48 | # for idx in tqdm(tiles.keys()):
|
46 | 49 | for idx in tiles.keys():
|
47 | | - k = [edge_has_fit(tiles, idx, i) for i in range(4)] |
48 | | - res.append((idx, k, len([e for e in k if e is not None]))) |
| 50 | + ns, ts = zip(*[edge_has_fit(tiles, idx, i) for i in range(4)]) |
| 51 | + tile = Tile(idx, ns, ts, len([e for e in ns if e is not None])) |
| 52 | + res.append(tile) |
49 | 53 |
|
50 | 54 | # print(int(np.prod(corners)))
|
51 | 55 |
|
52 | | -corners = [(idx, n) for idx, n, e in res if e == 2] |
53 | | -edges = [(idx, n) for idx, n, e in res if e == 3] |
| 56 | +corners = [t for t in res if t.e == 2] |
| 57 | +edges = [t for t in res if t.e == 3] |
| 58 | +middles = [t for t in res if t.e == 4] |
| 59 | + |
| 60 | +print([(t.idx, t.ns) for t in corners]) |
| 61 | +print([(t.idx, t.ns) for t in edges]) |
| 62 | +print([(t.idx, t.ns) for t in middles]) |
| 63 | +print("\n\n") |
| 64 | + |
| 65 | +nrows = 3 |
| 66 | +ncols = 3 |
| 67 | + |
| 68 | +def rotate(l, n): |
| 69 | + return list(l[n:] + l[:n]) |
| 70 | + |
| 71 | +def flip_lr(l): |
| 72 | + return [l[0], l[3], l[2], l[1]] |
| 73 | + |
| 74 | +def flip_ud(l): |
| 75 | + return [l[2], l[1], l[0], l[3]] |
54 | 76 |
|
55 | | -print(corners) |
56 | | -print(edges) |
| 77 | +def find_and_pop(elems, n_idx): |
| 78 | + res = None |
| 79 | + del_idx = None |
| 80 | + for i, t in enumerate(elems): |
| 81 | + if t.idx == n_idx: |
| 82 | + res = t |
| 83 | + del_idx = i |
| 84 | + break |
| 85 | + else: |
| 86 | + raise RuntimeError(f"{t.idx} was not found in {elems}.") |
57 | 87 |
|
58 | | -# for (idx_tl, n_tl), (idx_tr, n_tl), (idx_bl, n_bl), (idx_br, n_bl) in permutations(corners): |
59 | | -# print(idx_tl) |
| 88 | + del elems[del_idx] |
| 89 | + return res |
| 90 | + |
| 91 | +def match(ns, exp): |
| 92 | + assert len(ns) == len(exp) |
| 93 | + |
| 94 | + for n, e in zip(ns, exp): |
| 95 | + if e is None: |
| 96 | + if n is not None: |
| 97 | + return False |
| 98 | + else: |
| 99 | + if e == 0: |
| 100 | + continue |
| 101 | + else: |
| 102 | + if n != e: |
| 103 | + return False |
| 104 | + return True |
| 105 | + |
| 106 | +def place(tile, exp): |
| 107 | + if tile.idx == 1171: |
| 108 | + print("orig") |
| 109 | + print(tile.ns) |
| 110 | + |
| 111 | + n = tile.ns |
| 112 | + t = tile.ts |
| 113 | + for trans in product([0,1,2,3], [False, True], [False, True]): |
| 114 | + n = rotate(n, trans[0]) |
| 115 | + t = rotate(t, trans[0]) |
| 116 | + if trans[1]: |
| 117 | + n = flip_lr(n) |
| 118 | + t = flip_lr(t) |
| 119 | + if trans[2]: |
| 120 | + n = flip_ud(n) |
| 121 | + t = flip_ud(t) |
| 122 | + |
| 123 | + if tile.idx == 1171: |
| 124 | + print(trans[0], "lr?", trans[1], "ud?", trans[2], end=" ") |
| 125 | + print(n, exp) |
| 126 | + if match(n, exp): |
| 127 | + return Tile(tile.idx, n, t, tile.e) |
60 | 128 |
|
| 129 | + raise RuntimeError(tile) |
| 130 | + |
| 131 | +def print_short(l): |
| 132 | + print([(t.idx, t.ns) for t in l]) |
| 133 | + |
| 134 | +layout = [] |
| 135 | + |
| 136 | +for idx_row in range(nrows): |
| 137 | + if idx_row == 0: |
| 138 | + tile = corners.pop() |
| 139 | + tile = place(tile, [None, 0, 0, None]) |
| 140 | + row = [tile] |
| 141 | + elif idx_row == nrows-1: |
| 142 | + tile = find_and_pop(corners, layout[idx_row-1][0].ns[2]) |
| 143 | + upper = layout[idx_row-1][0].idx |
| 144 | + tile = place(tile, [upper, 0, None, None]) |
| 145 | + row = [tile] |
| 146 | + else: |
| 147 | + row = [find_and_pop(edges, layout[idx_row-1][0])] |
| 148 | + |
| 149 | + for idx_col in range(1, ncols): |
| 150 | + prev_tile = row[-1] |
| 151 | + n_idx = prev_tile.ns[1] |
| 152 | + |
| 153 | + print_short(row) |
| 154 | + print(idx_col, n_idx) |
| 155 | + |
| 156 | + if idx_row in (0, nrows-1): |
| 157 | + if idx_col == ncols-1: |
| 158 | + elems = corners |
| 159 | + else: |
| 160 | + elems = edges |
| 161 | + else: |
| 162 | + if idx_col == ncols-1: |
| 163 | + elems = edges |
| 164 | + else: |
| 165 | + elem = middles |
| 166 | + |
| 167 | + t = find_and_pop(elems, n_idx) |
| 168 | + |
| 169 | + if idx_row == 0: |
| 170 | + upper = None |
| 171 | + else: |
| 172 | + upper = layout[idx_row-1][0].idx |
| 173 | + |
| 174 | + left = prev_tile.idx |
| 175 | + |
| 176 | + if idx_col == ncols-1: |
| 177 | + if idx_row == 0: |
| 178 | + row.append(place(t, [None, None, 0, left])) |
| 179 | + |
| 180 | + elif idx_row == nrows-1: |
| 181 | + row.append(place(t, [0, None, None, left])) |
| 182 | + |
| 183 | + else: |
| 184 | + row.append(place(t, [upper, None, 0, left])) |
| 185 | + else: |
| 186 | + row.append(place(t, [upper, 0, 0, left])) |
| 187 | + |
| 188 | + layout.append(row) |
61 | 189 |
|
| 190 | +print(layout) |
0 commit comments