# The receipt tamper from the Rabbit Hole Generator's challenge, unchanged:
# it mutates the FIRST primitive value it finds, wherever that is.
import json
r = json.load(open('receipt.json'))

def mutate(obj):
    if isinstance(obj, dict):
        for k in list(obj.keys()):
            if isinstance(obj[k], (str, int, float, bool)):
                if isinstance(obj[k], str):
                    obj[k] = obj[k] + '-tampered'
                elif isinstance(obj[k], bool):
                    obj[k] = not obj[k]
                else:
                    obj[k] = obj[k] + 1
                return True
            if mutate(obj[k]):
                return True
    elif isinstance(obj, list):
        for i in range(len(obj)):
            if isinstance(obj[i], (str, int, float, bool)):
                if isinstance(obj[i], str):
                    obj[i] = obj[i] + '-tampered'
                elif isinstance(obj[i], bool):
                    obj[i] = not obj[i]
                else:
                    obj[i] = obj[i] + 1
                return True
            if mutate(obj[i]):
                return True
    return False

assert mutate(r), 'No mutable primitive found in receipt.json'
json.dump(r, open('receipt_tampered.json', 'w'), indent=2)
print('tampered receipt written: receipt_tampered.json')
