File size: 2,883 Bytes
848d4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
"""
Validator for dts_7_5_min_scope: Minimum-Scope (7,5)-Difference Triangle Set

Expected input:
{
  "n": 7,
  "k": 5,
  "rows": [
    [0, ..., ..., ..., ..., ...],
    ...
    (7 rows total)
  ]
}

Validity:
- n == 7, k == 5
- Each row length == k+1 == 6
- Each row is strictly increasing and starts with 0
- All positive within-row differences are distinct across ALL rows

Metric:
- scope = max entry in the array (minimize)
"""

import argparse
from typing import Any

from . import ValidationResult, load_solution, output_result, success, failure


def validate(sol: Any) -> ValidationResult:
    try:
        if not isinstance(sol, dict):
            return failure("Invalid format: expected dict.")

        n = int(sol.get("n", -1))
        k = int(sol.get("k", -1))
        rows = sol.get("rows", None)

        if n != 7 or k != 5:
            return failure("This benchmark requires n=7 and k=5 exactly.")

        if not isinstance(rows, list) or len(rows) != n:
            return failure(f"'rows' must be a list of length {n}.")

        # Check rows and collect differences
        seen_diffs = set()
        scope = 0

        for i, row in enumerate(rows):
            if not isinstance(row, list) or len(row) != k + 1:
                return failure(f"Row {i} must be a list of length {k+1}.")

            # Check integers and increasing
            try:
                r = [int(x) for x in row]
            except Exception:
                return failure(f"Row {i} contains non-integer values.")

            if r[0] != 0:
                return failure(f"Row {i} must start with 0 (normalized).")
            for j in range(1, k + 1):
                if r[j] <= r[j - 1]:
                    return failure(f"Row {i} must be strictly increasing.")

            scope = max(scope, r[-1])

            # Positive differences within this row
            for a in range(k + 1):
                for b in range(a):
                    d = r[a] - r[b]  # positive since increasing and a>b
                    if d <= 0:
                        return failure("Non-positive difference encountered (should be impossible).")
                    if d in seen_diffs:
                        return failure(f"Duplicate difference {d} found (violates DTS property).")
                    seen_diffs.add(d)

        metrics = {"scope": scope}
        return success("Valid (7,5)-DTS.", metrics=metrics)

    except Exception as e:
        return failure(f"Exception during validation: {e}")


def main():
    parser = argparse.ArgumentParser(description="Validate a (7,5)-Difference Triangle Set")
    parser.add_argument("solution", help="Solution as JSON string or path to JSON file")
    args = parser.parse_args()

    sol = load_solution(args.solution)
    result = validate(sol)
    output_result(result)


if __name__ == "__main__":
    main()