#!/usr/bin/env python3 """Evaluator for dev-intervals (dev set; public). usage: evaluator.py WORKSPACE LANGUAGE TASK_DIR -> JSON verdict on stdout """ import os import random import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import devlib # noqa: E402 LIMIT = 1 << 63 def number(tok): if not tok or not tok.isascii() or not tok.isdigit() or (len(tok) > 1 and tok[0] == "0"): return None v = int(tok) return v if v < LIMIT else None def model(text): """Reference semantics: the sorted list of disjoint, non-touching intervals.""" ivs, out = [], [] lines = text.split("\n") if text.endswith("\n"): lines.pop() if text == "": lines = [] for line in lines: parts = line.split(" ") cmd, args = parts[0], parts[1:] nums = [number(a) for a in args] if None in nums: out.append("error") elif cmd in ("add", "remove") and len(nums) == 2 and nums[0] < nums[1]: a, b = nums keep = [] for x, y in ivs: if cmd == "remove" or y < a or x > b: if y <= a or x >= b: keep.append((x, y)) else: if x < a: keep.append((x, a)) if y > b: keep.append((b, y)) else: a, b = min(a, x), max(b, y) if cmd == "add": keep.append((a, b)) ivs = sorted(keep) elif cmd == "covered" and len(nums) == 1: out.append("yes" if any(x <= nums[0] < y for x, y in ivs) else "no") elif cmd == "count" and not args: out.append(str(len(ivs))) elif cmd == "total" and not args: out.append(str(sum(y - x for x, y in ivs))) elif cmd == "list" and not args: out.append(" ".join(f"{x}-{y}" for x, y in ivs) or "empty") else: out.append("error") return "".join(o + "\n" for o in out) def gen(rng, n, lo, hi, invalid=0.0): lines = [] for _ in range(n): r = rng.random() if r < invalid: lines.append(rng.choice(["", "add", "add 1", "add 3 3", "add 5 2", "remove 01 4", "add -1 4", "covered", "covered x", "count 1", "list now", "total ", " total", "add 1 2", "Add 1 2", f"add 0 {LIMIT}", f"covered {LIMIT + 5}", "add 1 2 3", "add\t1 2", "remove 2 1"])) continue k = rng.random() a = rng.randrange(lo, hi) b = a + rng.randrange(1, max(2, (hi - lo) // 8)) if k < 0.35: lines.append(f"add {a} {b}") elif k < 0.55: lines.append(f"remove {a} {b}") elif k < 0.75: lines.append(f"covered {rng.randrange(lo, hi)}") else: lines.append(rng.choice(["count", "total", "list"])) return "\n".join(lines) + ("\n" if rng.random() < 0.8 else "") def case(text): return (text, model(text)) PUBLIC = [case("add 10 20\nadd 20 25\ncovered 24\nremove 12 14\nlist\ncount\ntotal\nadd 5 5\n")] rng = random.Random(20261007) HIDDEN = [ case(""), case("list\ncount\ntotal\ncovered 0\n"), case("add 0 1\nadd 2 3\nadd 1 2\nlist\nremove 0 3\nlist\n"), case(f"add 0 {LIMIT - 1}\ntotal\ncovered {LIMIT - 2}\ncovered {LIMIT - 1}\nlist\nremove 1 {LIMIT - 2}\nlist\ntotal"), case("add 5 10\nremove 0 100\nadd 1 2\nadd 3 4\nadd 5 6\nadd 2 3\nlist\ncount\nadd 4 5\nlist\n"), case("add 7 9\n\nadd 9 11\nlist\nadd 1 2 \nlist"), case(gen(rng, 60, 0, 100, invalid=0.25)), case(gen(rng, 400, 0, 1000, invalid=0.05)), case(gen(rng, 700, 0, 1 << 40, invalid=0.02)), case("\n".join(f"add {2 * i} {2 * i + 1}" for i in range(1400)) + "\ncount\ntotal\ncovered 2799\ncovered 2798\n"), ] if __name__ == "__main__": devlib.evaluate(devlib.Spec("intervals", heap=4 << 20, fuel=400_000_000, public=PUBLIC, hidden=HIDDEN))