File size: 4,026 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""
Validator for problem 065: General Algorithm for Difference Bases

Test an algorithm that produces difference bases at multiple values of n.

Expected input format:
    {
        "algorithm": "description",
        "test_cases": [
            {"n": n, "basis": [b0, b1, ...]},
            ...
        ]
    }
"""

import argparse
import math
from typing import Any

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


def baseline_ratio(n: int) -> float:
    """Compute baseline efficiency: (2 * ceil(sqrt(n)))^2 / n."""
    return (2 * math.ceil(math.sqrt(n))) ** 2 / n


def verify_difference_basis(n: int, basis: list[int]) -> tuple[bool, int]:
    """Verify a difference basis and return (valid, size)."""
    B = set(basis)

    # Check range
    for b in B:
        if b < 0 or b >= n:
            return False, len(B)

    # Compute all differences
    differences = set()
    for a in B:
        for b in B:
            diff = abs(a - b)
            if diff > 0:
                differences.add(diff)

    # Check coverage
    missing = set(range(1, n)) - differences
    return len(missing) == 0, len(B)


def validate(solution: Any) -> ValidationResult:
    """
    Validate a general difference basis algorithm.

    Args:
        solution: Dict with algorithm and test cases

    Returns:
        ValidationResult with performance analysis
    """
    try:
        if not isinstance(solution, dict):
            return failure("Invalid format: expected dict")

        algorithm = solution.get('algorithm', 'not provided')
        test_cases = solution.get('test_cases', [])

        if not test_cases:
            return failure("Need at least one test case")

    except (ValueError, TypeError) as e:
        return failure(f"Failed to parse solution: {e}")

    results = []
    all_valid = True
    beats_baseline_count = 0

    for tc in test_cases:
        n = int(tc['n'])
        basis = [int(b) for b in tc['basis']]

        valid, size = verify_difference_basis(n, basis)
        ratio = (size ** 2) / n
        bl_ratio = baseline_ratio(n)
        beats = valid and ratio < bl_ratio

        if not valid:
            all_valid = False
        if beats:
            beats_baseline_count += 1

        results.append({
            'n': n,
            'basis_size': size,
            'ratio': ratio,
            'baseline_ratio': bl_ratio,
            'beats_baseline': beats,
            'valid': valid
        })

    if not all_valid:
        invalid = [r for r in results if not r['valid']]
        return failure(
            f"Invalid difference basis for n={invalid[0]['n']}",
            test_results=results
        )

    avg_ratio = sum(r['ratio'] for r in results) / len(results)
    metrics = dict(
        algorithm=algorithm,
        test_results=results,
        average_ratio=avg_ratio,
        beats_baseline_count=beats_baseline_count,
        num_test_cases=len(results),
    )

    if beats_baseline_count == 0:
        details = [f"n={r['n']}: ratio={r['ratio']:.4f} vs baseline={r['baseline_ratio']:.4f}" for r in results[:5]]
        return failure(
            f"Valid bases but none beat the baseline (need |B|²/n < (2*ceil(sqrt(n)))²/n). "
            f"{'; '.join(details)}",
            **metrics,
        )

    return success(
        f"Difference basis algorithm valid for all {len(results)} test cases "
        f"(avg |B|²/n: {avg_ratio:.4f}). "
        f"Beats baseline in {beats_baseline_count}/{len(results)} test cases.",
        **metrics,
    )


def main():
    parser = argparse.ArgumentParser(description='Validate general difference basis algorithm')
    parser.add_argument('solution', help='Solution as JSON string or path to JSON file')
    parser.add_argument('--verbose', '-v', action='store_true', help='Verbose output')
    args = parser.parse_args()

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


if __name__ == '__main__':
    main()