-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathabcd_test.py
176 lines (143 loc) · 5.05 KB
/
abcd_test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
### Explanation
1. **Imports and Type Definitions**:
- `itertools.product` is used to generate all possible combinations of answers.
- `Literal` and `Tuple` from `typing` are used for type hinting.
2. **Predicate Functions**:
- Functions `p0` to `p9` represent the conditions for each question in the logic puzzle. Each function takes a `choice` (the answer for that question) and a `hypo` (a tuple representing a hypothesis of answers for all questions) and returns whether the choice satisfies the condition for that question.
3. **Testing Hypotheses**:
- `test_hypo` function iterates over all questions and checks if the given hypothesis satisfies all conditions.
4. **Main Function**:
- `main` function generates all possible combinations of answers using `product('ABCD', repeat=10)` and tests each combination using `test_hypo`. If a combination satisfies all conditions, it prints the answers.
"""
from itertools import product
from typing import Literal, Tuple
ChoiceType = Literal['A', 'B', 'C', 'D']
def p0(choice: ChoiceType, hypo: Tuple[ChoiceType]):
return True
def p1(choice: ChoiceType, hypo: Tuple[ChoiceType]):
if choice == 'A':
return hypo[4] == 'C'
if choice == 'B':
return hypo[4] == 'D'
if choice == 'C':
return hypo[4] == 'A'
if choice == 'D':
return hypo[4] == 'B'
return None
def p2(choice: ChoiceType, hypo: Tuple[ChoiceType]):
if choice == 'A':
return hypo[2] != hypo[5] and hypo[2] != hypo[1] and hypo[2] != hypo[3]
if choice == 'B':
return hypo[5] != hypo[2] and hypo[5] != hypo[1] and hypo[5] != hypo[3]
if choice == 'C':
return hypo[1] != hypo[2] and hypo[1] != hypo[5] and hypo[1] != hypo[3]
if choice == 'D':
return hypo[3] != hypo[2] and hypo[3] != hypo[5] and hypo[3] != hypo[1]
return None
def p3(choice: ChoiceType, hypo: Tuple[ChoiceType]):
if choice == 'A':
return hypo[0] == hypo[4]
if choice == 'B':
return hypo[1] == hypo[6]
if choice == 'C':
return hypo[0] == hypo[8]
if choice == 'D':
return hypo[5] == hypo[9]
return None
def p4(choice: ChoiceType, hypo: Tuple[ChoiceType]):
if choice == 'A':
return hypo[4] == hypo[7]
if choice == 'B':
return hypo[4] == hypo[1]
if choice == 'C':
return hypo[4] == hypo[8]
if choice == 'D':
return hypo[4] == hypo[6]
return None
def p5(choice: ChoiceType, hypo: Tuple[ChoiceType]):
if choice == 'A':
return hypo[7] == hypo[1] and hypo[7] == hypo[3]
if choice == 'B':
return hypo[7] == hypo[0] and hypo[7] == hypo[5]
if choice == 'C':
return hypo[7] == hypo[2] and hypo[7] == hypo[9]
if choice == 'D':
return hypo[7] == hypo[4] and hypo[7] == hypo[8]
return None
def p6(choice: ChoiceType, hypo: Tuple[ChoiceType]):
na, nb, nc, nd = 0, 0, 0, 0
for x in hypo:
if x == 'A':
na += 1
if x == 'B':
nb += 1
if x == 'C':
nc += 1
if x == 'D':
nd += 1
min_opt = min(na, nb, nc, nd)
if choice == 'A':
return nc == min_opt
if choice == 'B':
return nb == min_opt
if choice == 'C':
return na == min_opt
if choice == 'D':
return nd == min_opt
return None
def p7(choice: ChoiceType, hypo: Tuple[ChoiceType]):
x = ord(hypo[0])
if choice == 'A':
return abs(x - ord(hypo[6])) != 1
if choice == 'B':
return abs(x - ord(hypo[4])) != 1
if choice == 'C':
return abs(x - ord(hypo[1])) != 1
if choice == 'D':
return abs(x - ord(hypo[9])) != 1
return None
def p8(choice: ChoiceType, hypo: Tuple[ChoiceType]):
a = hypo[0] == hypo[5]
if choice == 'A':
return a != hypo[5] == hypo[4]
if choice == 'B':
return a != hypo[9] == hypo[4]
if choice == 'C':
return a != hypo[1] == hypo[4]
if choice == 'D':
return a != hypo[8] == hypo[4]
return None
def p9(choice: ChoiceType, hypo: Tuple[ChoiceType]):
na, nb, nc, nd = 0, 0, 0, 0
for x in hypo:
if x == 'A':
na += 1
if x == 'B':
nb += 1
if x == 'C':
nc += 1
if x == 'D':
nd += 1
delta = max(na, nb, nc, nd) - min(na, nb, nc, nd)
if choice == 'A':
return delta == 3
if choice == 'B':
return delta == 2
if choice == 'C':
return delta == 4
if choice == 'D':
return delta == 1
return None
def test_hypo(problem_list, hypo) -> bool:
for i, p in enumerate(problem_list):
if not p(hypo[i], hypo):
return False
return True
def main():
problem_list = (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9)
for hypo in product('ABCD', repeat=len(problem_list)):
if test_hypo(problem_list, hypo):
print('Answers:', hypo)
if __name__ == '__main__':
main()