세이브 더 월드

파이썬 너무 좋아 파이썬에 밥말아먹고 파이썬으로 샤워해야지
mail@pastecode.io avatar
unknown
python
a year ago
1.8 kB
10
Indexable
Never
from __future__ import annotations

import random
from contextlib import nullcontext
from typing import TextIO


def gen_small(
    n: int, seed: int | None = None, file: str | TextIO | None = None
) -> list[tuple[int, int]]:
    random.seed(seed)
    points = [
        (i, j) for i in range(-15, 16) for j in range(-15, 16) if (i, j) != (0, 0)
    ]
    random.shuffle(points)
    chosen = points[:n]
    out = (
        open(file, "w", encoding="utf-8")
        if isinstance(file, str)
        else nullcontext(file)
    )
    with out as f:
        print(n, file=f)
        for x, y in chosen:
            print(x, y, file=f)
    return chosen


def gen_large(
    n: int, seed: int | None = None, file: str | TextIO | None = None
) -> list[tuple[int, int]]:
    random.seed(seed)
    points = set()

    while len(points) < n:
        x = random.randint(-1000, 1000)
        y = random.randint(-1000, 1000)
        if (x, y) == (0, 0):
            continue
        points.add((x, y))

    out = (
        open(file, "w", encoding="utf-8")
        if isinstance(file, str)
        else nullcontext(file)
    )
    with out as f:
        print(n, file=f)
        for x, y in points:
            print(x, y, file=f)
    return list(points)


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("n", type=int)
    parser.add_argument("-l", "--large", action="store_true")
    parser.add_argument("-s", "--seed", type=int, default=None)
    parser.add_argument("-f", "--file", type=str, default=None)
    args = parser.parse_args()
    if args.large:
        gen_large(args.n, seed=args.seed, file=args.file)
    else:
        gen_small(args.n, seed=args.seed, file=args.file)