"""Search for a box-free 2-colouring of the n x n x n grid with a SAT solver. A box is {x1, x2} x {y1, y2} x {z1, z2}; it must not have all 8 cells of one colour. The search is restricted to colourings invariant under the diagonal shift (x, y, z) -> (x+step, y+step, z+step) mod n and, with "rot", under (x, y, z) -> (y, z, x). One Boolean variable per orbit; each box gives two clauses (not all true, not all false). Needs: pip install python-sat Usage: python3 generator.py n step [rot] e.g. python3 generator.py 12 2 rot Writes cube{n}_step{step}[_rot].txt: n blocks (layers z) of n lines (rows y) of n characters (x), with LF line endings on every platform. """ import sys, time from itertools import combinations, product from pysat.solvers import Solver n, step = int(sys.argv[1]), int(sys.argv[2]) rot = len(sys.argv) > 3 and sys.argv[3] == "rot" orbit = {} for p in product(range(n), repeat=3): if p in orbit: continue label = len(set(orbit.values())) + 1 stack = [p] orbit[p] = label while stack: x, y, z = stack.pop() images = [((x + step) % n, (y + step) % n, (z + step) % n)] if rot: images.append((y, z, x)) for q in images: if q not in orbit: orbit[q] = label stack.append(q) pairs = list(combinations(range(n), 2)) clauses = set() for x1, x2 in pairs: for y1, y2 in pairs: for z1, z2 in pairs: vs = sorted({orbit[(x, y, z)] for x in (x1, x2) for y in (y1, y2) for z in (z1, z2)}) clauses.add(tuple(vs)) clauses.add(tuple(-v for v in vs)) print(f"n={n} step={step} rot={rot} orbits={len(set(orbit.values()))} clauses={len(clauses)}", flush=True) t = time.time() with Solver(name="cadical195", bootstrap_with=[list(c) for c in clauses]) as s: ok = s.solve() print("SAT" if ok else "UNSAT", f"{time.time() - t:.1f} s", flush=True) if ok: true = {l for l in s.get_model() if l > 0} name = f"cube{n}_step{step}{'_rot' if rot else ''}.txt" with open(name, "w", newline="\n") as fh: for z in range(n): for y in range(n): fh.write("".join("1" if orbit[(x, y, z)] in true else "0" for x in range(n)) + "\n") fh.write("\n") print("wrote", name)