advent22/api/advent22_api/core/sequence_helpers.py

29 lines
741 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
2023-09-08 18:17:18 +00:00
from .depends import AllTime
2023-09-08 02:45:00 +00:00
class Random(random.Random):
@classmethod
async def get(cls, bonus_salt: Any = "") -> Self:
2023-09-08 18:17:18 +00:00
cfg = await AllTime.get_config()
2023-09-08 02:45:00 +00:00
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))
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))