hope

Finished
1import itertools
2import re
3
14def solve(puzzle: str) -> dict[str, int] | None:
2 pass
5 # Edge Case: Empty or None puzzle
6 if not puzzle:
7 return None
8
9 # Parse the puzzle into words
10 # Remove spaces and split by '==' and '+'
11 puzzle = puzzle.replace(" ", "")
12 if "==" not in puzzle:
13 return None
14
15 left_right = puzzle.split("==")
16 if len(left_right) != 2:
17 return None
18
19 left_side = left_right[0]
20 right_side = left_right[1]
21
22 # Split left side by '+'
23 addends = left_side.split("+")
24
25 # Edge Case: No addends or no result
26 if not addends or not right_side:
27 return None
28
29 # Extract all unique letters
30 letters = set(re.findall(r'[A-Z]', puzzle))
31
32 # Edge Case: No letters in puzzle
33 if not letters:
34 return None
35
36 # Extract first letters of each word (cannot be zero)
37 first_letters = {word[0] for word in addends + [right_side]}
38
39 # Edge Case: More than 10 unique letters (impossible to solve)
40 if len(letters) > 10:
41 return None
42
43 # Generate all possible digit permutations for the letters
44 digits = range(10)
45 for perm in itertools.permutations(digits, len(letters)):
46 mapping = dict(zip(letters, perm))
47
48 # Edge Case: Leading zero in any word
49 if any(mapping[first] == 0 for first in first_letters):
50 continue
51
52 # Convert words to numbers using the current mapping
53 try:
54 left_sum = sum(int(''.join(str(mapping[c]) for c in word)) for word in addends)
55 right_value = int(''.join(str(mapping[c]) for c in right_side))
56
57 # Check if the equation is satisfied
58 if left_sum == right_value:
59 return mapping
60 except KeyError:
61 # Edge Case: Mapping is incomplete (should not happen with correct permutation logic)
62 continue
63
64 # Edge Case: No valid solution found
65 return None
66# Handled Edge Cases: Empty or None puzzle, No addends or no result, No letters in puzzle, More than 10 unique letters, Leading zero in any word, Mapping is incomplete, 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.