import dataclasses import decimal import re from comprehension_history import ARCHITECT, DIMENSIONS, JUNIOR, MID, Seat, SENIOR from failure import Failure SCORES = re.compile(", ".join(rf"{name} ([0-9]+(?:\.[0-9]+)?)" for name in DIMENSIONS)) FIRST_SEATS = (MID, ARCHITECT) SCORING_SEATS = (JUNIOR, MID, SENIOR, ARCHITECT) SEAT_LINE = re.compile(rf"^({'|'.join(SCORING_SEATS)}): (.+)$") SPLIT_SEATS = (SENIOR, JUNIOR) VOTE_LINE = re.compile(", ".join(rf"{name} (better|same|worse)" for name in DIMENSIONS)) @dataclasses.dataclass(frozen=True) class SeatsFile: """What the seats read, ` at `, with `, against ` for a paired ruling, then each seat's line and its places.""" read: str seats: list[Seat] def seats_file(text: str) -> SeatsFile | Failure: lines = [line for line in text.splitlines() if line.strip()] if not lines: return Failure("the seats file is empty; its first line names what the seats read: at ") seats: list[Seat] = [] for line in lines[1:]: if seat := SEAT_LINE.match(line): if seat.group(1) in [known.name for known in seats]: return Failure(f"{seat.group(1)} appears twice in the seats file") seats.append(Seat(seat.group(1), seat.group(2).strip(), [])) elif seats and line.startswith("- "): seats[-1].places.append(line[2:].strip()) elif seats and seats[-1].places and line.startswith(" "): seats[-1].places[-1] += " " + line.strip() else: return Failure(f"the seats file has a line that is neither a seat's line, opening with one of {', '.join(SCORING_SEATS)} and a colon, nor one of its places: {line}") return SeatsFile(lines[0].strip(), seats) def scores(seats: list[Seat]) -> dict[str, list[str]] | Failure: """Each seat's five scores as written, in DIMENSIONS' order.""" if [seat.name for seat in seats] != list(SCORING_SEATS): return Failure(f"a scoring run's seats are {', '.join(SCORING_SEATS)}, in that order; the file has {', '.join(seat.name for seat in seats) or 'none'}") found = {} for seat in seats: values = score_line(seat.name, seat.said) if isinstance(values, Failure): return values found[seat.name] = values return found def score_line(name: str, said: str) -> list[str] | Failure: """A line's five scores as written, each from 1 to 10 and the overall at most 1 above the lowest dimension.""" if not (written := SCORES.fullmatch(said)): return Failure(f"{name}'s line is not {', '.join(score + ' ' for score in DIMENSIONS)}: {said}") values = [decimal.Decimal(value) for value in written.groups()] if not all(1 <= value <= 10 for value in values): return Failure(f"{name}'s scores are not each from 1 to 10: {said}") if values[-1] > min(values[:-1]) + 1: return Failure(f"{name}'s overall exceeds the lowest dimension by more than 1; ask the seat to rescore: {said}") return list(written.groups()) def vote_line(name: str, said: str) -> list[str] | Failure: """A paired seat's five votes, in DIMENSIONS' order.""" if not (written := VOTE_LINE.fullmatch(said)): return Failure(f"{name}'s line is not {', '.join(vote + ' ' for vote in DIMENSIONS)}: {said}") return list(written.groups())