-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
82 lines (60 loc) · 1.83 KB
/
app.py
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
73
74
75
76
77
78
79
80
81
82
import streamlit as st
from simpleai.search import CspProblem, backtrack
st.title("Cryptographic Puzzle")
st.header("Challenge AI")
word1 = st.text_input('First Word', 'TWO')
word2 = st.text_input('Second Word', 'TWO')
word3 = st.text_input('Solution Word', 'FOUR')
letters = []
domains = {}
for i in word1:
if i == word1[0]:
domains[i] = list(range(1, 10))
letters.append(i)
else:
domains[i] = list(range(0, 10))
letters.append(i)
for j in word2:
if j == word2[0]:
domains[j] = list(range(1, 10))
letters.append(j)
else:
domains[j] = list(range(0, 10))
letters.append(j)
for k in word3:
if k == word3[0]:
domains[k] = list(range(1, 10))
letters.append(k)
else:
domains[k] = list(range(0, 10))
letters.append(k)
letters = list(set(letters))
def constraint_unique(letters, values):
return len(values) == len(set(values)) # remove repeated values and count
def constraint_add(letters, values):
factor1 = ""
factor2 = ""
solution = ""
for char in word1:
pos = letters.index(char)
factor1 += str(values[pos])
for char in word2:
pos = letters.index(char)
factor2 += str(values[pos])
for char in word3:
pos = letters.index(char)
solution += str(values[pos])
factor1 = int(factor1)
factor2 = int(factor2)
solution = int(solution)
return (factor1 + factor2) == solution
constraints = [
(letters, constraint_unique),
(letters, constraint_add),
]
problem = CspProblem(letters, domains, constraints)
if st.button('Run'):
output = backtrack(problem)
st.write('\nSolutions:', output)
#output = backtrack(problem)
#print('\nSolutions:', output)