mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-23 16:23:00 +00:00
28 lines
741 B
Python
28 lines
741 B
Python
import itertools
|
|
import random
|
|
from typing import Any, Self, Sequence
|
|
|
|
from .depends import AllTime
|
|
|
|
|
|
class Random(random.Random):
|
|
@classmethod
|
|
async def get(cls, bonus_salt: Any = "") -> Self:
|
|
cfg = await AllTime.get_config()
|
|
return cls(f"{cfg.puzzle.solution}{bonus_salt}{cfg.puzzle.random_pepper}")
|
|
|
|
|
|
async def shuffle(seq: Sequence, rnd: random.Random | None = None) -> list:
|
|
# Zufallsgenerator
|
|
rnd = rnd or await Random.get()
|
|
|
|
# Elemente mischen
|
|
return rnd.sample(seq, len(seq))
|
|
|
|
|
|
def set_len(seq: Sequence, length: int) -> list:
|
|
# `seq` unendlich wiederholen
|
|
infinite = itertools.cycle(seq)
|
|
|
|
# Die ersten `length` einträge nehmen
|
|
return list(itertools.islice(infinite, length))
|