2023-09-08 18:17:18 +00:00
|
|
|
import itertools
|
2023-09-08 02:45:00 +00:00
|
|
|
import random
|
2023-09-12 02:58:10 +00:00
|
|
|
from typing import Any, Self, Sequence, TypeVar
|
2023-09-08 02:45:00 +00:00
|
|
|
|
2023-09-08 19:53:35 +00:00
|
|
|
from .config import get_config
|
2023-09-08 02:45:00 +00:00
|
|
|
|
2023-09-12 02:58:10 +00:00
|
|
|
T = TypeVar("T")
|
|
|
|
|
2023-09-08 02:45:00 +00:00
|
|
|
|
|
|
|
class Random(random.Random):
|
|
|
|
@classmethod
|
|
|
|
async def get(cls, bonus_salt: Any = "") -> Self:
|
2023-09-08 19:53:35 +00:00
|
|
|
cfg = await get_config()
|
2023-09-11 22:14:33 +00:00
|
|
|
return cls(f"{cfg.puzzle.solution}{cfg.puzzle.random_seed}{bonus_salt}")
|
2023-09-08 02:45:00 +00:00
|
|
|
|
2023-09-12 02:58:10 +00:00
|
|
|
def shuffled(self, population: Sequence[T]) -> Sequence[T]:
|
|
|
|
return self.sample(population, k=len(population))
|
|
|
|
|
2023-09-08 02:45:00 +00:00
|
|
|
|
|
|
|
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))
|
2023-09-08 18:17:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
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))
|