seahawk

Finished
11def solve(puzzle: str) -> dict[str, int] | None:
2 pass
2 """
3 Solve an alphametics puzzle.
4
5 Args:
6 puzzle: A string representing an alphametics puzzle in the format "A + B == C"
7
8 Returns:
9 A dictionary mapping letters to digits that solves the puzzle, or None if no solution exists.
10
11 Example:
12 >>> solve("SEND + MORE == MONEY")
13 {'S': 9, 'E': 5, 'N': 6, 'D': 7, 'M': 1, 'O': 0, 'R': 8, 'Y': 2}
14 """
15 # Edge Case: Empty or None puzzle
16 if not puzzle:
17 return None
18
19 # Parse the puzzle
20 parts = puzzle.replace("==", "=").replace(" ", "").split("=")
21
22 # Edge Case: Invalid puzzle format
23 if len(parts) != 2:
24 return None
25
26 left_side, right_side = parts[0], parts[1]
27
28 # Edge Case: Empty sides
29 if not left_side or not right_side:
30 return None
31
32 # Split the left side by '+' to get addends
33 addends = left_side.split("+")
34
35 # Edge Case: No addends
36 if not addends:
37 return None
38
39 # Extract all unique letters
40 letters = set()
41 for part in addends + [right_side]:
42 letters.update(part)
43
44 # Edge Case: No letters in puzzle
45 if not letters:
46 return None
47
48 # Extract first letters (can't be zero)
49 first_letters = {word[0] for word in addends + [right_side]}
50
51 # Edge Case: More than 10 unique letters (impossible to solve)
52 if len(letters) > 10:
53 return None
54
55 # Convert letters to a list for indexing
56 letters_list = list(letters)
57
58 # Try all permutations of digits for the letters
59 from itertools import permutations
60
61 # Edge Case: Handle single digit words
62 for perm in permutations(range(10), len(letters)):
63 mapping = dict(zip(letters_list, perm))
64
65 # Check if any first letter is assigned zero
66 if any(mapping[letter] == 0 for letter in first_letters):
67 continue
68
69 # Convert words to numbers using the mapping
70 try:
71 addend_values = []
72 for addend in addends:
73 value = 0
74 for char in addend:
75 value = value * 10 + mapping[char]
76 addend_values.append(value)
77
78 right_value = 0
79 for char in right_side:
80 right_value = right_value * 10 + mapping[char]
81
82 # Check if the sum is correct
83 if sum(addend_values) == right_value:
84 return mapping
85 except KeyError:
86 # Edge Case: Mapping is incomplete (should not happen with proper permutation)
87 continue
88
89 # Edge Case: No solution found
90 return None
91
92# Handled Edge Cases: Empty or None puzzle, Invalid puzzle format, Empty sides, No addends, No letters in puzzle, More than 10 unique letters, Mapping is incomplete, No 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.