Kratos

Finished
1from itertools import permutations
2
13def solve(puzzle: str) -> dict[str, int] | None:
2 pass
4 """
5 Solve an alphametics puzzle.
6
7 Args:
8 puzzle: A string representing the alphametics puzzle (e.g., "SEND + MORE == MONEY")
9
10 Returns:
11 A dictionary mapping letters to digits that solves the puzzle, or None if no solution exists.
12
13 The function handles the following edge cases:
14 - Invalid puzzle format (no '==')
15 - Words with leading zeros
16 - No valid solutions
17 """
18 # Edge Case: Invalid puzzle format (no '==')
19 if "==" not in puzzle:
20 return None
21
22 # Split the puzzle into left and right sides
23 left_side, right_side = puzzle.split("==")
24 left_side = left_side.strip()
25 right_side = right_side.strip()
26
27 # Extract all words (alphanumeric sequences)
28 words = []
29 for part in left_side.split("+"):
30 words.append(part.strip())
31 words.append(right_side)
32
33 # Get all unique letters
34 letters = set()
35 for word in words:
36 for char in word:
37 if char.isalpha():
38 letters.add(char)
39
40 # Edge Case: Too many unique letters (more than 10)
41 if len(letters) > 10:
42 return None
43
44 # Get first letters of each word (can't be zero)
45 first_letters = {word[0] for word in words if word and word[0].isalpha()}
46
47 # Convert letters to a list for indexing
48 letters_list = list(letters)
49
50 # Try all digit permutations for the letters
51 for perm in permutations(range(10), len(letters)):
52 mapping = dict(zip(letters_list, perm))
53
54 # Edge Case: Leading zeros in any word
55 if any(mapping.get(word[0], 0) == 0 for word in words if len(word) > 1 and word[0].isalpha()):
56 continue
57
58 # Convert words to numbers
59 numbers = []
60 for word in words:
61 num_str = ""
62 for char in word:
63 if char.isalpha():
64 if char in mapping:
65 num_str += str(mapping[char])
66 else:
67 # This shouldn't happen, but just in case
68 num_str += char
69 else:
70 num_str += char
71
72 # Edge Case: Non-alphabetic characters in word that aren't digits
73 try:
74 numbers.append(int(num_str))
75 except ValueError:
76 # Invalid character in word
77 break
78
79 # Edge Case: Not all words were converted successfully
80 if len(numbers) != len(words):
81 continue
82
83 # Check if the sum of left side equals the right side
84 if sum(numbers[:-1]) == numbers[-1]:
85 return mapping
86
87 # Edge Case: No valid solution found
88 return None
89# Handled Edge Cases: Invalid puzzle format (no '=='), Words with leading zeros, No valid solutions, Too many unique letters (more than 10), Non-alphabetic characters in word that aren't digits, Not all words were converted successfully
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.