"""Check a weighted projection bound for the Matchbox silhouettes (stdlib only).""" from itertools import product from pathlib import Path import sys # Rows are second projected coordinate; columns are first. Dot is blank. WEIGHTS = ( ( '.01111111.', '1111111111', '111....111', '11......11', '11......11', '11......11', '22......22', '222....222', '2222122222', '.22212222.', ), ( '1111..1102', '1111..1111', '1111..1111', '1111..1111', '..111111..', '..111111..', '....00....', '....00....', '....00....', '....00....', ), ( '0000000000', '0000000000', '....0000..', '....0000..', '....1100..', '....0000..', '00..0000..', '00..0000..', '00000000..', '00000000..', ), ) H = set(tuple(map(int, line.split())) for line in Path(sys.argv[1]).read_text().splitlines()) ROOT = (8, 1, 0) assert len(H) == 240 and ROOT in H project = lambda p, k: p[:k] + p[k+1:] assert min(sum(project(p, k) == project(ROOT, k) for p in H) for k in range(3)) == 1 weights = [] for k, rows in enumerate(WEIGHTS): assert len(rows) == 10 and all(len(row) == 10 for row in rows) weights.append({(i, j): int(c) for j, row in enumerate(rows) for i, c in enumerate(row) if c != '.'}) assert set(weights[k]) == {project(p, k) for p in H} assert weights[k][project(ROOT, k)] == 0 # Free edges: the two vertical rails in x=4,5 plus the root's upper crossbar. free = {((i, j), (i, j+1)) for i, j in product((4, 5), range(9))} free.add(((8, 1), (9, 1))) def edge(p, q): return tuple(sorted((project(p, 2), project(q, 2)))) all_edges = set() number = 0 for p, k, sign in product(sorted(H), range(3), (-1, 1)): q = p[:k] + (p[k] + sign,) + p[k+1:] if q not in H: continue if k != 2: all_edges.add(edge(p, q)) if q == ROOT: continue # the tree is directed away from this cube toll = sum(weights[j][project(q, j)] for j in range(3) if j != k) if k != 2 and edge(p, q) not in free: toll += 1 assert toll <= 2, (p, q, toll) number += 1 assert free <= all_edges assert H == {p for p in product(range(10), repeat=3) if all(project(p, k) in weights[k] for k in range(3))} mark_total = sum(sum(w.values()) for w in weights) edge_total = len(weights[2]) - 1 - len(free) assert (number, mark_total, edge_total) == (967, 135, 44) print(f'{number} arrows checked, at most 2 units apiece') print(f'Tree needs at least {mark_total}+{edge_total}={mark_total+edge_total} units') print(f'Minimum cube count is at least {1 + (mark_total+edge_total+1)//2}')