Skip to content

Instantly share code, notes, and snippets.

@quotepilgrim
Created December 20, 2023 18:46
Show Gist options
  • Select an option

  • Save quotepilgrim/af1d58131c132ee247ab887d42d57a55 to your computer and use it in GitHub Desktop.

Select an option

Save quotepilgrim/af1d58131c132ee247ab887d42d57a55 to your computer and use it in GitHub Desktop.
import random
MULTIPLIER = 2
QUEUE_SIZE = 5
PIECES = ("I", "J", "L", "O", "S", "T", "Z")
BAG_SIZE = len(PIECES) * MULTIPLIER + 1
CYCLE_LENGTH = BAG_SIZE * len(PIECES)
primary_queue = []
secondary_queue = []
next_queue = []
def create_bag(pieces, multiplier=MULTIPLIER):
return [pieces[i] for _ in range(multiplier) for i in range(len(pieces))]
def shuffle_bag(bag, check_repeats=True):
bag = random.sample(bag, len(bag))
while check_repeats:
check_repeats = False
for i in range(len(bag)):
if bag[i] == bag[i - 1]:
check_repeats = True
bag.insert(random.randrange(len(bag)), bag.pop(i))
return bag
def algorithm(bag):
global primary_queue, secondary_queue, next_queue
if primary_queue == []:
if secondary_queue == []:
secondary_queue = shuffle_bag(PIECES, check_repeats=False)
extra_piece = secondary_queue.pop()
primary_queue = shuffle_bag(bag + [extra_piece])
next_queue.append(primary_queue.pop())
return next_queue.pop(0)
bag = create_bag(PIECES)
next_queue = [algorithm(bag) for _ in range(QUEUE_SIZE)]
output = []
for i in range(CYCLE_LENGTH * 3):
output.append(algorithm(bag))
if (i + 1) % BAG_SIZE == 0:
print(
" ".join(output),
" ".join({i for i in output if output.count(i) > MULTIPLIER}),
sep=" : ",
)
output = []
if (i + 1) % CYCLE_LENGTH == 0:
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment