#!/usr/bin/env python3
"""
Extract the flag by deobfuscating the baby.py challenge.
The key is understanding the XOR operations and validation logic.
"""

# Import the functions from baby.py without running the main entry point
import sys
import importlib.util

spec = importlib.util.spec_from_file_location("baby", "baby.py")
baby = importlib.util.module_from_spec(spec)

# Execute the module to get all the functions defined
spec.loader.exec_module(baby)

# Now we have access to all the functions. Let's try calling the validator
# with various inputs to extract the correct flag

# First, let's check what the success message decodes to
print("Testing the validation function...")

# The main validation function is gog0sQu1D
# It takes user input and validates it

# Let's try some common flag formats
test_flags = [
    "flag{test}",
    "flag",
    "test",
    "BABY",
    "baby",
]

for test in test_flags:
    print(f"Testing: {test}")
    try:
        # We'd need to manually call the validation logic
        # For now, just try to understand the structure
        pass
    except Exception as e:
        print(f"Error: {e}")

# Alternative: Let's look at the structure to understand what we need
# The flag appears to be embedded in the XOR operations

print("\nLooking at the challenge structure...")

# Based on my analysis, the success message characters are:
success_codes = [99, 85, 79, 26, 93]  # cUO... 
print(f"First 5 success chars: {[chr(c) if 32 <= c < 127 else f'[{c}]' for c in success_codes]}")

# This doesn't look like a typical message, suggesting the extraction was incomplete
# Let me try a different strategy - look for the actual validation input length

# Find all character comparisons in the validation logic
with open('baby.py', 'r') as f:
    content = f.read()

# The flag length is likely checked first
# Count the gOg0sQuId calls in the success message to get the flag length
import re
matches = re.findall(r'gOg0sQuId\(', content)
print(f"\nTotal gOg0sQuId calls in file: {len(matches)}")

# The input validation appears to check each character
# Let's try to find the pattern

# Look for patterns like "if" statements that validate individual characters
validation_patterns = re.findall(r'if.*?:', content[:5000])
print(f"\nFound {len(validation_patterns)} validation patterns in first 5000 chars")
