advent22/api/advent22_api/routers/_misc.py

20 lines
511 B
Python

import itertools
import random
from typing import Sequence
from ..dav_common import dav_get_file
async def get_loesungswort() -> str:
fp = await dav_get_file("loesungswort.txt")
return fp.read().decode("utf8").strip()
async def shuffle(seq: Sequence, length: int | None = None) -> list:
rnd = random.Random(await get_loesungswort())
if length is not None:
infinity = itertools.cycle(seq)
seq = list(itertools.islice(infinity, length))
return rnd.sample(seq, len(seq))