-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_thirty_three.py
More file actions
72 lines (59 loc) · 2.09 KB
/
unit_thirty_three.py
File metadata and controls
72 lines (59 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pop quiz
# Check if a number is a perfect square
def is_perfect_square(n):
if n < 0:
return False
x = n // 2
seen = set([x])
while x * x != n:
x = (x + (n // x)) // 2
if x in seen:
return False
seen.add(x)
return x * x == n
# Calculate the numeric value for a word based on the current mapping
def word_value(word, mapping):
return int(''.join(str(mapping[char]) for char in word))
# Check if the sum of digits of a number is a perfect square
def sum_of_digits_is_square(number):
digit_sum = sum(int(digit) for digit in str(number))
return is_perfect_square(digit_sum)
# Backtracking function to assign digits to letters
def solve_puzzle(words, mapping, used_digits):
if len(mapping) == len(set(''.join(words))): # All letters are assigned
for word in words:
value = word_value(word, mapping)
if not (is_perfect_square(value) and sum_of_digits_is_square(value)):
return False
return True
# Get the next letter to assign
for letter in set(''.join(words)):
if letter not in mapping:
break
# Try each digit for this letter
for digit in range(10):
if digit not in used_digits:
mapping[letter] = digit
used_digits.add(digit)
# Recursive call to continue with the next letter
if solve_puzzle(words, mapping, used_digits):
return True
# Backtrack
del mapping[letter]
used_digits.remove(digit)
return False
# Main function to setup the problem and find the solution
def main():
words = ["A", "MERRY", "XMAS", "TO", "ALL"]
# Pre-assign the known values
initial_mapping = {'T': 8, 'O': 1}
used_digits = set(initial_mapping.values())
# Solve the puzzle
if solve_puzzle(words, initial_mapping, used_digits):
print("Solution found:")
for letter, digit in sorted(initial_mapping.items()):
print(f"{letter} = {digit}")
else:
print("No solution found.")
if __name__ == "__main__":
main()