| 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 an 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 | try: |
| 20 | # Edge Case: Invalid puzzle format |
| 21 | if "==" not in puzzle and "=" not in puzzle: |
| 22 | return None |
| 23 | |
| 24 | # Handle both == and = operators |
| 25 | if "==" in puzzle: |
| 26 | left_side, right_side = puzzle.replace(" ", "").split("==") |
| 27 | else: |
| 28 | left_side, right_side = puzzle.replace(" ", "").split("=") |
| 29 | |
| 30 | # Edge Case: Empty sides |
| 31 | if not left_side or not right_side: |
| 32 | return None |
| 33 | except ValueError: |
| 34 | # Handle malformed puzzle format |
| 35 | return None |
| 36 | |
| 37 | # Handle addition specifically |
| 38 | if "+" in left_side: |
| 39 | addends = left_side.split("+") |
| 40 | else: |
| 41 | addends = [left_side] |
| 42 | |
| 43 | # Edge Case: Validate all parts contain only alphabetic characters |
| 44 | for part in addends + [right_side]: |
| 45 | if not part.isalpha(): |
| 46 | return None |
| 47 | |
| 48 | # Extract all unique letters |
| 49 | letters = set() |
| 50 | for part in addends + [right_side]: |
| 51 | letters.update(part) |
| 52 | |
| 53 | # Edge Case: No letters in puzzle |
| 54 | if not letters: |
| 55 | return None |
| 56 | |
| 57 | # Extract first letters (can't be zero) |
| 58 | first_letters = {word[0] for word in addends + [right_side]} |
| 59 | |
| 60 | # Edge Case: More than 10 unique letters (impossible to solve) |
| 61 | if len(letters) > 10: |
| 62 | return None |
| 63 | |
| 64 | # Try all digit permutations for the letters |
| 65 | digits = range(10) |
| 66 | for perm in permutations(digits, len(letters)): |
| 67 | mapping = dict(zip(letters, perm)) |
| 68 | |
| 69 | # Edge Case: Leading zero |
| 70 | if any(mapping[letter] == 0 for letter in first_letters): |
| 71 | continue |
| 72 | |
| 73 | # Convert words to numbers |
| 74 | addend_values = [] |
| 75 | for addend in addends: |
| 76 | value = 0 |
| 77 | for letter in addend: |
| 78 | value = value * 10 + mapping[letter] |
| 79 | addend_values.append(value) |
| 80 | |
| 81 | result_value = 0 |
| 82 | for letter in right_side: |
| 83 | result_value = result_value * 10 + mapping[letter] |
| 84 | |
| 85 | # Check if the equation is satisfied |
| 86 | if sum(addend_values) == result_value: |
| 87 | return mapping |
| 88 | |
| 89 | # Edge Case: No solution found |
| 90 | return None |
| 91 | # Handled Edge Cases: Invalid puzzle format, empty sides, no addends, no letters in puzzle, more than 10 unique letters, leading zero, no solution found |