| 1 | | def solve(puzzle: str) -> dict[str, int] | None: |
| 2 | | pass |
| 1 | from itertools import permutations |
| 2 | from typing import Dict, Optional |
| 3 | |
| 4 | def 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: Empty or None puzzle |
| 20 | if not puzzle: |
| 21 | return None |
| 22 | |
| 23 | # Remove spaces and split by '==' |
| 24 | parts = puzzle.replace(" ", "").split("==") |
| 25 | |
| 26 | # Edge Case: Invalid puzzle format (no '==' or incorrect number of parts) |
| 27 | if len(parts) != 2: |
| 28 | return None |
| 29 | |
| 30 | left_side, right_side = parts[0], parts[1] |
| 31 | |
| 32 | # Split the left side by '+' to get addends |
| 33 | addends = left_side.split("+") |
| 34 | |
| 35 | # Edge Case: No addends on the left side |
| 36 | if not addends or not all(addends): |
| 37 | return None |
| 38 | |
| 39 | # Edge Case: Empty right side |
| 40 | if not right_side: |
| 41 | return None |
| 42 | |
| 43 | # Extract all unique letters |
| 44 | letters = set() |
| 45 | for part in addends + [right_side]: |
| 46 | letters.update(part) |
| 47 | |
| 48 | # Edge Case: No letters in puzzle |
| 49 | if not letters: |
| 50 | return None |
| 51 | |
| 52 | # Extract first letters (can't be zero) |
| 53 | first_letters = {word[0] for word in addends + [right_side]} |
| 54 | |
| 55 | # Edge Case: More than 10 unique letters (impossible to assign unique digits) |
| 56 | if len(letters) > 10: |
| 57 | return None |
| 58 | |
| 59 | # Try all digit permutations for the letters |
| 60 | digits = range(10) |
| 61 | for perm in permutations(digits, len(letters)): |
| 62 | mapping = dict(zip(letters, perm)) |
| 63 | |
| 64 | # Edge Case: Leading zero in any word |
| 65 | if any(mapping[first_letter] == 0 for first_letter in first_letters): |
| 66 | continue |
| 67 | |
| 68 | # Convert words to numbers |
| 69 | try: |
| 70 | addend_values = [] |
| 71 | for addend in addends: |
| 72 | value = 0 |
| 73 | for char in addend: |
| 74 | value = value * 10 + mapping[char] |
| 75 | addend_values.append(value) |
| 76 | |
| 77 | right_value = 0 |
| 78 | for char in right_side: |
| 79 | right_value = right_value * 10 + mapping[char] |
| 80 | |
| 81 | # Check if the sum is correct |
| 82 | if sum(addend_values) == right_value: |
| 83 | return mapping |
| 84 | except KeyError: |
| 85 | # Edge Case: Mapping is incomplete (shouldn't happen with correct permutation logic) |
| 86 | continue |
| 87 | |
| 88 | # Edge Case: No valid solution found |
| 89 | return None |
| 90 | |
| 91 | # Handled Edge Cases: Empty or None puzzle, Invalid puzzle format, No addends, Empty right side, No letters, More than 10 unique letters, Leading zero in any word, No valid solution found |