Alone

Finished
1def solve(puzzle: str) -> dict[str, int] | None:
2 pass
1from itertools import permutations
2from typing import Dict, Optional
3
4def solve(puzzle: str) -> Optional[Dict[str, int]]:
5 """
6 Solve an alphametics puzzle.
7
8 Args:
9 puzzle: A string representing the alphametics puzzle (e.g., "SEND + MORE = MONEY")
10
11 Returns:
12 A dictionary mapping letters to digits that solves the puzzle, or None if no solution exists.
13
14 Example:
15 >>> solve("SEND + MORE = MONEY")
16 {'S': 9, 'E': 5, 'N': 6, 'D': 7, 'M': 1, 'O': 0, 'R': 8, 'Y': 2}
17 """
18 # Parse the puzzle
19 # Edge Case: Invalid puzzle format
20 if '=' not in puzzle:
21 return None
22
23 # Remove spaces and split by '='
24 puzzle_clean = puzzle.replace(' ', '')
25 left_side, right_side = puzzle_clean.split('=')
26
27 # Edge Case: Empty sides
28 if not left_side or not right_side:
29 return None
30
31 # Determine the operator and parse operands
32 if '*' in left_side:
33 # Multiplication case: "AB * C = DEF"
34 operands = left_side.split('*')
35 operator = '*'
36 else:
37 # Addition case: "A + B = C" or "A + B + C = D"
38 operands = left_side.split('+')
39 operator = '+'
40
41 # Edge Case: No operands
42 if not operands:
43 return None
44
45 # Extract all unique letters
46 letters = set()
47 for part in operands + [right_side]:
48 letters.update(part)
49
50 # Edge Case: No letters in puzzle
51 if not letters:
52 return None
53
54 # Extract first letters of multi-digit numbers (can't be zero)
55 first_letters = {word[0] for word in operands + [right_side] if len(word) > 1}
56
57 # Edge Case: More than 10 unique letters (impossible to solve)
58 if len(letters) > 10:
59 return None
60
61 # Try all digit permutations for the letters
62 digits = range(10)
63 for perm in permutations(digits, len(letters)):
64 mapping = dict(zip(letters, perm))
65
66 # Edge Case: Leading zero in multi-digit numbers
67 if any(mapping[letter] == 0 for letter in first_letters):
68 continue
69
70 # Convert words to numbers
71 operand_values = []
72 for operand in operands:
73 value = 0
74 for letter in operand:
75 value = value * 10 + mapping[letter]
76 operand_values.append(value)
77
78 result_value = 0
79 for letter in right_side:
80 result_value = result_value * 10 + mapping[letter]
81
82 # Check if the equation is satisfied
83 if operator == '+':
84 if sum(operand_values) == result_value:
85 return mapping
86 elif operator == '*':
87 product = 1
88 for value in operand_values:
89 product *= value
90 if product == result_value:
91 return mapping
92
93 # Edge Case: No solution found
94 return None
95
96# Handled Edge Cases: Invalid puzzle format, empty sides, no addends, no letters in puzzle, more than 10 unique letters, leading zero, no solution found
Test NameStatus
test_leading_zero_solution_is_invalid
Fail
test_puzzle_with_eight_letters
Fail
test_puzzle_with_four_letters
Fail
test_puzzle_with_seven_letters
Fail
test_puzzle_with_six_letters
Fail
test_puzzle_with_ten_letters
Fail
test_puzzle_with_ten_letters_and_199_addends
Fail
test_puzzle_with_three_letters
Fail
test_puzzle_with_two_digits_final_carry
Fail
test_solution_must_have_unique_value_for_each_letter
Fail

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