| 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 | # Edge Case: Invalid puzzle format (no equality) |
| 24 | if "==" not in puzzle: |
| 25 | return None |
| 26 | |
| 27 | # Split into left and right sides |
| 28 | left_side, right_side = puzzle.replace("==", "=").split("=") |
| 29 | |
| 30 | # Edge Case: Malformed puzzle with multiple equals |
| 31 | if "=" in left_side: |
| 32 | return None |
| 33 | |
| 34 | # Extract only alphabetic words from the entire puzzle |
| 35 | import re |
| 36 | words = re.findall(r'[A-Z]+', puzzle) |
| 37 | |
| 38 | # Edge Case: No words in puzzle |
| 39 | if not words: |
| 40 | return None |
| 41 | |
| 42 | # Get unique letters |
| 43 | unique_letters = set("".join(words)) |
| 44 | |
| 45 | # Edge Case: More than 10 unique letters (impossible to solve) |
| 46 | if len(unique_letters) > 10: |
| 47 | return None |
| 48 | |
| 49 | # Get first letters of each word (can't be zero) |
| 50 | first_letters = {word[0] for word in words} |
| 51 | |
| 52 | # Split left side into operands based on operators |
| 53 | # Find the operator |
| 54 | operators = ['*', '+', '-'] |
| 55 | operator = None |
| 56 | for op in operators: |
| 57 | if op in left_side: |
| 58 | operator = op |
| 59 | break |
| 60 | |
| 61 | # If no operator found, assume it's a single operand |
| 62 | if operator is None: |
| 63 | operands = [left_side.strip()] |
| 64 | else: |
| 65 | operands = [word.strip() for word in left_side.split(operator) if word.strip()] |
| 66 | |
| 67 | # Edge Case: No operands |
| 68 | if not operands: |
| 69 | return None |
| 70 | |
| 71 | # Edge Case: Right side is not a single word |
| 72 | result_word = right_side.strip() |
| 73 | if not result_word.isalpha(): |
| 74 | return None |
| 75 | |
| 76 | # Try all permutations of digits for the unique letters |
| 77 | for perm in permutations(range(10), len(unique_letters)): |
| 78 | mapping = dict(zip(unique_letters, perm)) |
| 79 | |
| 80 | # Edge Case: Leading zero in any word |
| 81 | if any(mapping[word[0]] == 0 for word in words): |
| 82 | continue |
| 83 | |
| 84 | # Calculate the values of operands |
| 85 | operand_values = [] |
| 86 | for operand in operands: |
| 87 | value = 0 |
| 88 | for char in operand: |
| 89 | value = value * 10 + mapping[char] |
| 90 | operand_values.append(value) |
| 91 | |
| 92 | # Calculate the result value |
| 93 | result_value = 0 |
| 94 | for char in result_word: |
| 95 | result_value = result_value * 10 + mapping[char] |
| 96 | |
| 97 | # Check if the equation is satisfied based on the operator |
| 98 | equation_satisfied = False |
| 99 | if operator is None: |
| 100 | # Single operand case |
| 101 | equation_satisfied = operand_values[0] == result_value |
| 102 | elif operator == '+': |
| 103 | equation_satisfied = sum(operand_values) == result_value |
| 104 | elif operator == '*': |
| 105 | product = 1 |
| 106 | for val in operand_values: |
| 107 | product *= val |
| 108 | equation_satisfied = product == result_value |
| 109 | elif operator == '-': |
| 110 | # For subtraction, we expect operand1 - operand2 = result |
| 111 | if len(operand_values) == 2: |
| 112 | equation_satisfied = operand_values[0] - operand_values[1] == result_value |
| 113 | |
| 114 | if equation_satisfied: |
| 115 | return mapping |
| 116 | |
| 117 | # Edge Case: No solution found |
| 118 | return None |
| 119 | # Handled Edge Cases: Empty or None puzzle, Invalid puzzle format (no equality), Malformed puzzle with multiple equals, No words in puzzle, More than 10 unique letters, No addends, Right side is not a single word, Leading zero in any word, No solution found |