Proving two quizzes aren't the same quiz: canonical forms and a small layout search
In an earlier post I described how the fan quizzes on cv.cm use one balanced layout (twelve questions, four options each, eight results, six slots per result) and reuse it by renaming the results and reordering each question's options. Four options have only 24 orders, so this week the second base layout ran out, and I had to find a third one.
Two problems came up that are fun on their own:
- How do you check that two quizzes are not secretly the same layout, when one may have renamed results and shuffled questions?
- How do you search for a new layout that meets every balance rule and is not one of the old ones in disguise?
When are two layouts "the same"?
A quiz's score is a sum, so the order of the questions does not matter. Result names don't matter either. Two quizzes mirror each other if some renaming of results makes one quiz's set of questions equal to the other's, where a question is its options in order.
That gives a canonical form: try all 8! = 40,320 renamings, write each question as a string of letters, sort the questions, and keep the smallest result. Two quizzes are the same exactly when their canonical forms match.
const canonical = (rows: string[][]) => { const names = [...new Set(rows.flat())].sort(); let best = ""; for (const p of permutations(8)) { const name = new Map(names.map((n, i) => [n, String.fromCharCode(97 + p[i])])); const form = rows.map((r) => r.map((n) => name.get(n)).join("")).sort().join("|"); if (!best || form < best) best = form; } return best; }; 40,320 × 12 short strings per quiz runs in well under a second, and the test computes it for every eight-result quiz on the site (about 70 of them) and fails if two share a form, apart from an explicit allowlist of old quizzes that predate the rule.
The rules a new layout must meet
- Every result appears in exactly 6 of the 48 slots.
- Every result sits in each answer position once or twice, so skimmers who always pick A don't favor anyone.
- Every pair of results meets in two or three questions.
- No two questions split the cast into two halves (their union is never all eight).
A small search
I split the search into three steps: first which four results go in each question (ignoring order), then a check against the old layouts, then the order inside each question.
Step 1, the sets. Start from 12 random 4-subsets of the 8 results and score the violations: replication off from 6, pairs meeting fewer than 2 or more than 3 times, complementary questions. Then hill-climb: replace one random question with a random 4-subset and keep the change if the score doesn't get worse. A valid design turns up within seconds.
Step 2, reject old designs. Compute the canonical form of the unordered design and compare it with the existing base layouts. If it matches one, no reordering can make it new, so throw it away. I also count automorphisms, the renamings that map the design to itself. I only keep designs with exactly one (the identity). With a symmetric design, two different option orders could turn out to be the same layout under a renaming, and you'd get fewer than 24 usable orders.
Step 3, the positions. Shuffle each question's options and swap pairs inside a question until every result is in each position one or two times. This converges quickly too.
def assign(blocks): rows = [list(b) for b in blocks] while True: bad = position_violations(rows) if bad == 0: return rows r = random.choice(rows) i, j = random.sample(range(4), 2) r[i], r[j] = r[j], r[i] if position_violations(rows) > bad: r[i], r[j] = r[j], r[i] # undo Using it
The new layout became the Gintama quiz, and its rotations and reorderings now carry the Doraemon, Crayon Shin-chan, Uma Musume and Zootopia quizzes. The layout tests from the first post run on it too, along with the canonical-form check against everything else on the site.
Since this design has no symmetry, all 24 orders are distinct, which should cover the next few batches before a fourth search is needed.
All the quizzes are on the cv.cm tests page. They run in the page with no sign-up and show your scores for every character, not just the winner.