#!/usr/bin/env python3
"""Verify reconstructed exact counts against a residue file for one extra prime."""
from __future__ import annotations

import argparse
from pathlib import Path


def load_values(path: Path) -> list[int]:
    values: list[int] = []
    for line in path.read_text().splitlines():
        line = line.strip()
        if not line or line.startswith('#'):
            continue
        k_s, value_s = line.split()
        k = int(k_s)
        if k != len(values):
            raise ValueError(f'Unexpected index {k} in {path}')
        values.append(int(value_s))
    return values


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument('--counts', type=Path, required=True)
    parser.add_argument('--residue-file', type=Path, required=True)
    parser.add_argument('--prime', type=int, required=True)
    args = parser.parse_args()

    counts = load_values(args.counts)
    residues = load_values(args.residue_file)
    if len(counts) != len(residues):
        raise ValueError('Count and residue files have different lengths')

    for k, (count, residue) in enumerate(zip(counts, residues)):
        if count % args.prime != residue:
            raise AssertionError(f'Independent-prime mismatch at k={k}')
    print(f'All {len(counts)} values agree modulo {args.prime}.')


if __name__ == '__main__':
    main()
