File size: 2,063 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 | #!/usr/bin/env python3
"""
Validator for problem 092: Sum of Three Cubes for n = 390
Validates that integers x, y, z satisfy x³ + y³ + z³ = 390.
Expected input format:
{"x": <int>, "y": <int>, "z": <int>}
or [x, y, z]
"""
import argparse
from typing import Any
from . import ValidationResult, load_solution, parse_integer, output_result, success, failure
TARGET = 390
def validate(solution: Any) -> ValidationResult:
"""
Validate that the solution satisfies x³ + y³ + z³ = 390.
Args:
solution: Dict with keys x, y, z or list [x, y, z]
Returns:
ValidationResult with success/failure and computed sum
"""
try:
if isinstance(solution, dict):
x = parse_integer(solution['x'])
y = parse_integer(solution['y'])
z = parse_integer(solution['z'])
elif isinstance(solution, (list, tuple)) and len(solution) == 3:
x, y, z = [parse_integer(v) for v in solution]
else:
return failure(f"Invalid solution format: expected dict or list of 3 integers")
except (KeyError, ValueError, TypeError) as e:
return failure(f"Failed to parse solution: {e}")
result = x**3 + y**3 + z**3
if result == TARGET:
return success(
f"Verified: ({x})³ + ({y})³ + ({z})³ = {TARGET}",
x=str(x), y=str(y), z=str(z), sum=TARGET
)
else:
return failure(
f"Failed: ({x})³ + ({y})³ + ({z})³ = {result} ≠ {TARGET}",
x=str(x), y=str(y), z=str(z), computed_sum=result, target=TARGET
)
def main():
parser = argparse.ArgumentParser(description='Validate solution for sum of three cubes = 390')
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()
|