import sys
import json
import jsonschema

from collections.abc import Callable

def run_weekly_challenge(run_solution: Callable[[object], object], input_example: str, input_schema_json: str):
    inputs: list[str] = sys.argv[1:]

    validator = jsonschema.Draft202012Validator(json.loads(input_schema_json))

    input_errors = False

    for input_string in inputs:
        print(f'Inputs: {input_string}')

        try:
            input_object = json.loads(input_string)
        except Exception as inst:
            print(f'Error: invalid json: {inst}')
            input_errors = True
            continue
        try:
            validator.validate(input_object)
        except jsonschema.ValidationError as inst:
            print(f'Error: invalid input: {inst.message}')
            input_errors = True
            continue

        try:
            result = run_solution(input_object)
            print(f'Output: {result}')
        except Exception as inst:
            print(f'Exception: {inst}')

    if input_errors:
        print(f'Expected inputs arguments like {input_example}')
