who-are-you

Finished
1import itertools
2import re
3
14def solve(puzzle: str) -> dict[str, int] | None:
2 pass
5 # Remove spaces and split by '==' to get left and right parts
6 parts = puzzle.replace(" ", "").split("==")
7
8 # Edge Case: Invalid puzzle format with incorrect number of parts
9 if len(parts) != 2:
10 return None
11
12 left_side, right_side = parts
13
14 # Split the left side by '+' to get addends
15 addends = left_side.split("+")
16
17 # Extract all unique letters from the puzzle
18 letters = set(re.findall(r'[A-Z]', puzzle))
19
20 # Edge Case: No letters in puzzle
21 if not letters:
22 return None
23
24 # Extract first letters of each word (cannot be zero)
25 first_letters = {word[0] for word in addends + [right_side]}
26
27 # Edge Case: More than 10 unique letters (impossible to assign unique digits)
28 if len(letters) > 10:
29 return None
30
31 # Generate all possible digit permutations for the unique letters
32 for perm in itertools.permutations(range(10), len(letters)):
33 mapping = dict(zip(letters, perm))
34
35 # Edge Case: Leading zero in any word
36 if any(mapping[first_letter] == 0 for first_letter in first_letters):
37 continue
38
39 # Convert words to numbers using the current mapping
40 try:
41 addend_values = [int(''.join(str(mapping[c]) for c in word)) for word in addends]
42 right_value = int(''.join(str(mapping[c]) for c in right_side))
43 except KeyError:
44 # Edge Case: Mapping is incomplete (should not happen with correct permutation logic)
45 continue
46
47 # Check if the sum of addends equals the right side value
48 if sum(addend_values) == right_value:
49 return mapping
50
51 # Edge Case: No valid solution found
52 return None
53# Handled Edge Cases: Invalid puzzle format, no letters in puzzle, more than 10 unique letters, leading zero in any word, incomplete mapping, no valid solution found
Test NameStatus
test_leading_zero_solution_is_invalid
Pass
test_puzzle_with_eight_letters
Pass
test_puzzle_with_four_letters
Pass
test_puzzle_with_seven_letters
Pass
test_puzzle_with_six_letters
Pass
test_puzzle_with_ten_letters
Pass
test_puzzle_with_ten_letters_and_199_addends
Pass
test_puzzle_with_three_letters
Pass
test_puzzle_with_two_digits_final_carry
Pass
test_solution_must_have_unique_value_for_each_letter
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.