advent22/api/advent22_api/core/sequence_helpers.py

29 lines
733 B
Python
Raw Normal View History

2023-09-08 18:17:18 +00:00
import itertools
2023-09-08 02:45:00 +00:00
import random
from typing import Any, Self, Sequence
from .config import get_config
2023-09-08 02:45:00 +00:00
class Random(random.Random):
@classmethod
async def get(cls, bonus_salt: Any = "") -> Self:
cfg = await get_config()
return cls(f"{cfg.puzzle.solution}{cfg.puzzle.random_seed}{bonus_salt}")
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))