| 1 | ||
| 2 | import sys, pytest, collections, collections.abc, urllib3.exceptions, _pytest.pytester, numpy; | |
| 3 | collections.Mapping = collections.abc.Mapping; | |
| 4 | collections.MutableMapping = collections.abc.MutableMapping; | |
| 5 | collections.MutableSet = collections.abc.MutableSet; | |
| 6 | collections.Sequence = collections.abc.Sequence; | |
| 7 | collections.Callable = collections.abc.Callable; | |
| 8 | collections.Iterable = collections.abc.Iterable; | |
| 9 | collections.Iterator = collections.abc.Iterator; | |
| 10 | urllib3.exceptions.SNIMissingWarning = urllib3.exceptions.DependencyWarning; | |
| 11 | pytest.RemovedInPytest4Warning = DeprecationWarning; | |
| 12 | _pytest.pytester.Testdir = _pytest.pytester.Pytester; | |
| 13 | numpy.PINF = numpy.inf; | |
| 14 | numpy.unicode_ = numpy.str_; | |
| 15 | numpy.bytes_ = numpy.bytes_; | |
| 16 | numpy.float_ = numpy.float64; | |
| 17 | numpy.string_ = numpy.bytes_; | |
| 18 | numpy.NaN = numpy.nan; | |
| 19 | ||
| 20 | ||
| 21 | from itertools import permutations | |
| 22 | ||
| 23 | # Check classic example: SEND + MORE = MONEY | |
| 24 | letters = ['S', 'E', 'N', 'D', 'M', 'O', 'R', 'Y'] | |
| 25 | solutions = [] | |
| 26 | ||
| 27 | print("Checking SEND + MORE = MONEY:") | |
| 28 | for perm in permutations(range(10), 8): | |
| 29 | mapping = dict(zip(letters, perm)) | |
| 30 | ||
| 31 | # Check leading zero constraint for multi-digit words | |
| 32 | if mapping['S'] == 0 or mapping['M'] == 0: | |
| 33 | continue # S and M are first letters of multi-digit words | |
| 34 | ||
| 35 | # Calculate values | |
| 36 | SEND = mapping['S'] * 1000 + mapping['E'] * 100 + mapping['N'] * 10 + mapping['D'] | |
| 37 | MORE = mapping['M'] * 1000 + mapping['O'] * 100 + mapping['R'] * 10 + mapping['E'] | |
| 38 | MONEY = mapping['M'] * 10000 + mapping['O'] * 1000 + mapping['N'] * 100 + mapping['E'] * 10 + mapping['Y'] | |
| 39 | ||
| 40 | if SEND + MORE == MONEY: | |
| 41 | solutions.append(mapping) | |
| 42 | print(f"Found solution: {mapping}") | |
| 43 | print(f"SEND = {SEND}, MORE = {MORE}, MONEY = {MONEY}") | |
| 44 | print(f"{SEND} + {MORE} = {MONEY}") | |
| 45 | break | |
| 46 | ||
| 47 | if not solutions: | |
| 48 | print("No solutions found for SEND + MORE = MONEY") | |
| 49 | ||
| 50 | # Now let's do a full brute force for AB + BC = CA without any constraints | |
| 51 | count = 0 | |
| 52 | print("\nFull brute force for AB + BC = CA (this may take a while):") | |
| 53 | for perm in permutations(range(10), 3): | |
| 54 | count += 1 | |
| 55 | if count % 100 == 0: | |
| 56 | print(f"Checked {count} permutations...") | |
| 57 | ||
| 58 | mapping = dict(zip(['A', 'B', 'C'], perm)) | |
| 59 | ||
| 60 | # Calculate values | |
| 61 | AB = mapping['A'] * 10 + mapping['B'] | |
| 62 | BC = mapping['B'] * 10 + mapping['C'] | |
| 63 | CA = mapping['C'] * 10 + mapping['A'] | |
| 64 | ||
| 65 | if AB + BC == CA: | |
| 66 | print(f"Found solution: {mapping}") | |
| 67 | print(f"AB = {AB}, BC = {BC}, CA = {CA}") | |
| 68 | print(f"{AB} + {BC} = {CA}") | |
| 69 | ||
| 70 | # Check leading zero constraint | |
| 71 | has_leading_zero = False | |
| 72 | if mapping['A'] == 0: # A is first digit of AB | |
| 73 | has_leading_zero = True | |
| 74 | print("But A=0 makes AB a single digit number") | |
| 75 | if mapping['B'] == 0: # B is first digit of BC | |
| 76 | has_leading_zero = True | |
| 77 | print("But B=0 makes BC a single digit number") | |
| 78 | if mapping['C'] == 0: # C is first digit of CA | |
| 79 | has_leading_zero = True | |
| 80 | print("But C=0 makes CA a single digit number") | |
| 81 | ||
| 82 | if not has_leading_zero: | |
| 83 | print("This is a valid solution!") | |
| 84 | else: | |
| 85 | print("This violates leading zero constraint") | |
| 86 | ||
| 87 | print(f"Total permutations checked: {count}") |
| Test Name | Status |
|---|---|
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.