unicode

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 the alphametics puzzle (e.g., "SEND + MORE == MONEY")
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 # Parse the puzzle
16 if "==" not in puzzle:
17 return None
18
19 left_side, right_side = puzzle.split("==")
20 left_side = left_side.strip()
21 right_side = right_side.strip()
22
23 # Split the left side by '+' to get the addends
24 addends = [term.strip() for term in left_side.split("+")]
25
26 # Edge Case: Empty puzzle or missing components
27 if not addends or not right_side:
28 return None
29
30 # Get all unique letters in the puzzle
31 letters = set()
32 for term in addends + [right_side]:
33 letters.update(term)
34
35 # Edge Case: No letters in puzzle
36 if not letters:
37 return None
38
39 # Get the first letter of each term (can't be zero)
40 first_letters = {term[0] for term in addends + [right_side]}
41
42 # Edge Case: Invalid characters in puzzle (non-alphabetic)
43 if not all(c.isalpha() for c in letters):
44 return None
45
46 # Convert letters to a list for indexing
47 letters_list = list(letters)
48 n = len(letters_list)
49
50 # Edge Case: More than 10 unique letters (impossible to assign unique digits)
51 if n > 10:
52 return None
53
54 # Try all permutations of digits for the letters
55 from itertools import permutations
56
57 # Edge Case: Handle single letter terms
58 for perm in permutations(range(10), n):
59 # Create a mapping from letters to digits
60 mapping = dict(zip(letters_list, perm))
61
62 # Check if any first letter is assigned zero
63 if any(mapping[letter] == 0 for letter in first_letters):
64 continue
65
66 # Convert words to numbers using the mapping
67 try:
68 addend_values = []
69 for addend in addends:
70 value = 0
71 for char in addend:
72 value = value * 10 + mapping[char]
73 addend_values.append(value)
74
75 right_value = 0
76 for char in right_side:
77 right_value = right_value * 10 + mapping[char]
78
79 # Check if the sum is correct
80 if sum(addend_values) == right_value:
81 return mapping
82 except KeyError:
83 # Edge Case: Mapping is incomplete (shouldn't happen with proper permutation)
84 continue
85
86 # Edge Case: No valid solution found
87 return None
88
89# Handled Edge Cases: Empty puzzle or missing components, No letters in puzzle, Invalid characters in puzzle (non-alphabetic), More than 10 unique letters (impossible to assign unique digits), 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.