Skip to content

Instantly share code, notes, and snippets.

@graeme-winter
Created May 14, 2023 18:48
Show Gist options
  • Save graeme-winter/1d64092b52e5c34d547ceae4c0b178bc to your computer and use it in GitHub Desktop.
Save graeme-winter/1d64092b52e5c34d547ceae4c0b178bc to your computer and use it in GitHub Desktop.
Quilt combination generator - ensure each 3x3 block contains a unique permutation of 5 coloured pieces
import random
# FIXME fill in numbers of each unique piece here
tiles = {
"a": 10,
"b": 10,
"c": 10,
"d": 10,
"e": 10,
"f": 10,
"g": 10,
"h": 10,
"i": 10,
"j": 10,
"k": 10,
"l": 10,
"m": 10,
"n": 10,
"o": 10,
}
# then:
#
# population = "".join(x * tiles[x] for x in tiles)
#
# else generate random example instead - population of 140
letters = "abcdefghijklmno"
population = ""
while len(population) < 140:
population += random.choice(letters)
population = "".join(sorted(population))
squares = []
while len(squares) < 25:
square = set(random.choices(population, k=5))
if len(square) < 5:
# reject non unique
continue
if square in squares:
# reject duplicate
continue
# keep it
squares.append(square)
# remove the letters from population
for s in square:
i = population.index(s)
population = population[:i] + population[i + 1 :]
# passed
assert len(population) == 15
for j, square in enumerate(squares):
print(j + 1, "".join(sorted(square)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment