-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjindosh.py
executable file
·398 lines (326 loc) · 14.4 KB
/
jindosh.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python
import copy
import sys
# -------------------------------------------------------------------------------------------------------------------- #
# General riddling apparatus #
# -------------------------------------------------------------------------------------------------------------------- #
class Attribute(object):
def __init__(self, name, values):
object.__init__(self)
self.name = name
self.values = values
class AttributeValue(object):
def __init__(self, name, value):
object.__init__(self)
self.name = name
self.value = value
def __eq__(self, other):
return all([
self.name == other.name,
self.value == other.value
])
def __ne__(self, other):
return not self.__eq__(other)
class AttributeValueAlreadyAssociated(Exception):
def __init__(self):
Exception.__init__(self)
class Solution(object):
def __init__(self, attributes):
object.__init__(self)
self.__associations = {
attribute.name: {
attribute_value: {
associated_attribute.name:
None if attribute.name != associated_attribute.name
else attribute_value
for associated_attribute in attributes
}
for attribute_value in attribute.values
}
for attribute in attributes
}
def copy(self):
return copy.deepcopy(self)
def __repr__(self):
attribute_names_sorted = sorted(self.__attribute_names())
lines = []
for attribute_name in attribute_names_sorted:
attribute_lines = []
for attribute_value in sorted(self.__attribute_values(attribute_name)):
associated_attribute_values = []
for associated_attribute_name in attribute_names_sorted:
if attribute_name != associated_attribute_name:
associated_attribute_value = self.__associations[attribute_name][attribute_value][associated_attribute_name]
if associated_attribute_value is not None:
associated_attribute_values.append(associated_attribute_value)
if len(associated_attribute_values) > 0:
attribute_line = \
'{:<16} [ '.format(attribute_value).upper() \
+ ' | '.join([
'{:<16}'.format(associated_attribute_value)
for associated_attribute_value in associated_attribute_values
]) \
+ ']'
attribute_lines.append(attribute_line)
if len(attribute_lines) > 0:
lines.extend(attribute_lines)
lines.append('')
return '\n'.join(lines)
def __attribute_names(self):
return self.__associations.iterkeys()
def __attribute_values(self, attribute_name):
return self.__associations[attribute_name].iterkeys()
def associate(self, attribute_value_a, attribute_value_b):
for associated_value_a in self.__all_associated(attribute_value_a):
for associated_value_b in self.__all_associated(attribute_value_b):
self.__associate_pair(associated_value_a, associated_value_b)
def __all_associated(self, attribute_value):
return [
AttributeValue(associated_attribute_name, associated_attribute_value)
for associated_attribute_name, associated_attribute_value
in self.__associations[attribute_value.name][attribute_value.value].iteritems()
if associated_attribute_value
]
def __associate_pair(self, attribute_value_a, attribute_value_b):
# check if attributes are different
if attribute_value_a != attribute_value_b:
self.__associate_pair_one_way(attribute_value_a, attribute_value_b)
self.__associate_pair_one_way(attribute_value_b, attribute_value_a)
def __associate_pair_one_way(self, attribute_value_from, attribute_value_to):
current_association_value = self.__associations[attribute_value_from.name][attribute_value_from.value][attribute_value_to.name]
# check if the attribute is already associated
if current_association_value is not None:
# check if the attribute is associated to a different value
if current_association_value != attribute_value_to.value:
raise AttributeValueAlreadyAssociated()
else:
# associate the attribute
self.__associations[attribute_value_from.name][attribute_value_from.value][attribute_value_to.name] = attribute_value_to.value
def infer(self):
while True:
inferred_pairs = []
attribute_names_checked = []
for attribute_a_name in self.__attribute_names():
attribute_names_checked.append(attribute_a_name)
for attribute_a_value in self.__attribute_values(attribute_a_name):
for attribute_b_name in self.__attribute_names():
if all([
attribute_b_name not in attribute_names_checked,
self.__associations[attribute_a_name][attribute_a_value][attribute_b_name] is None
]):
attribute_b_possible_values = [
attribute_b_value
for attribute_b_value in self.__attribute_values(attribute_b_name)
if self.__is_association_possible(
attribute_a_name, attribute_a_value,
attribute_b_name, attribute_b_value
)
]
# single leftover value - let's associate it!
if len(attribute_b_possible_values) == 1:
attribute_b_value = attribute_b_possible_values[0]
inferred_pairs.append((
AttributeValue(attribute_a_name, attribute_a_value),
AttributeValue(attribute_b_name, attribute_b_value)
))
if len(inferred_pairs) == 0:
# we're not inferring anything anymore
break
for attribute_a_value, attribute_b_value in inferred_pairs:
self.associate(attribute_a_value, attribute_b_value)
def __is_association_possible(self, attribute_a_name, attribute_a_value, attribute_b_name, attribute_b_value):
for attribute_name in self.__attribute_names():
attribute_a_associated_value = self.__associations[attribute_a_name][attribute_a_value][attribute_name]
attribute_b_associated_value = self.__associations[attribute_b_name][attribute_b_value][attribute_name]
if all([
attribute_a_associated_value is not None,
attribute_b_associated_value is not None,
attribute_a_associated_value != attribute_b_associated_value
]):
return False
return True
def result(self, attribute_a_name, attribute_b_name):
return {
attribute_a_value: self.__associations[attribute_a_name][attribute_a_value][attribute_b_name]
for attribute_a_value in self.__attribute_values(attribute_a_name)
}
class Condition(object):
class Association(object):
def __init__(self, attribute_a_value, attribute_b_value):
object.__init__(self)
self.attribute_a_value = attribute_a_value
self.attribute_b_value = attribute_b_value
class Alternative(object):
def __init__(self, associations):
object.__init__(self)
self.associations = associations
def __init__(self, alternatives):
object.__init__(self)
self.alternatives = alternatives
def apply(self, solutions):
new_solutions = []
for solution in solutions:
for alternative, new_solution in zip(
self.alternatives,
[solution] + [solution.copy() for _ in xrange(len(self.alternatives) - 1)]
):
try:
for association in alternative.associations:
new_solution.associate(
association.attribute_a_value,
association.attribute_b_value
)
new_solution.infer()
new_solutions.append(new_solution)
except AttributeValueAlreadyAssociated:
pass
return new_solutions
def solve(attributes, conditions):
solutions = [Solution(attributes)]
for condition in conditions:
solutions = condition.apply(solutions)
return solutions
# -------------------------------------------------------------------------------------------------------------------- #
# Riddle-specific routines #
# -------------------------------------------------------------------------------------------------------------------- #
def is_same_person(attribute_a_value, attribute_b_value):
return Condition([
Condition.Alternative([
Condition.Association(attribute_a_value, attribute_b_value)
])
])
def sit_left_right(attribute_value_left, attribute_value_right):
seats_left_to_right = ['far left', 'center-left', 'center', 'center-right', 'far right']
return Condition([
Condition.Alternative([
Condition.Association(attribute_value_left, AttributeValue('seat', seats_left_to_right[left_seat_index])),
Condition.Association(attribute_value_right, AttributeValue('seat', seats_left_to_right[left_seat_index + 1]))
])
for left_seat_index in xrange(0, len(seats_left_to_right) - 1)
])
def sit_next_to(attribute_a_value, attribute_b_value):
left_right_alternatives = sit_left_right(attribute_a_value, attribute_b_value).alternatives
right_left_alternatives = sit_left_right(attribute_b_value, attribute_a_value).alternatives
return Condition(left_right_alternatives + right_left_alternatives)
def main():
# riddle attributes and rules - could be easily adapted for file input
attributes = [
Attribute(
'name',
['Lady Winslow', 'Doctor Marcolla', 'Countess Contee', 'Madam Natsiou', 'Baroness Finch']
),
Attribute(
'heirloom',
['Diamond', 'Ring', 'Bird Pendant', 'War Medal', 'Snuff Tin']
),
Attribute(
'color',
['purple', 'red', 'green', 'white', 'blue']
),
Attribute(
'drink',
['beer', 'rum', 'wine', 'whiskey', 'absinthe']
),
Attribute(
'city',
['Dunwall', 'Dabokva', 'Baleton', 'Fraeport', 'Karnaca']
),
Attribute(
'seat',
['far left', 'center-left', 'center', 'center-right', 'far right']
)
]
conditions = [
# Madam Natsiou - purple
is_same_person(
AttributeValue('name', 'Madam Natsiou'),
AttributeValue('color', 'purple')
),
# Lady Winslow - far left
is_same_person(
AttributeValue('name', 'Lady Winslow'),
AttributeValue('seat', 'far left')
),
# Lady Winslow - next to red
sit_next_to(
AttributeValue('name', 'Lady Winslow'),
AttributeValue('color', 'red')
),
# green - left to white
sit_left_right(
AttributeValue('color', 'green'),
AttributeValue('color', 'white')
),
# green - beer
is_same_person(
AttributeValue('color', 'green'),
AttributeValue('drink', 'beer')
),
# Dunwall - blue
is_same_person(
AttributeValue('city', 'Dunwall'),
AttributeValue('color', 'blue')
),
# Ring - next to Dunwall
sit_next_to(
AttributeValue('heirloom', 'Ring'),
AttributeValue('city', 'Dunwall')
),
# Doctor Marcolla - Diamond
is_same_person(
AttributeValue('name', 'Doctor Marcolla'),
AttributeValue('heirloom', 'Diamond')
),
# Dabokva - War Medal
is_same_person(
AttributeValue('city', 'Dabokva'),
AttributeValue('heirloom', 'War Medal')
),
# Snuff Tin - next to Baleton
sit_next_to(
AttributeValue('heirloom', 'Snuff Tin'),
AttributeValue('city', 'Baleton')
),
# Baleton - next to rum
sit_next_to(
AttributeValue('city', 'Baleton'),
AttributeValue('drink', 'rum')
),
# unsure: Snuff Tin - rum
is_same_person(
AttributeValue('heirloom', 'Snuff Tin'),
AttributeValue('drink', 'rum')
),
# Countess Contee - wine
is_same_person(
AttributeValue('name', 'Countess Contee'),
AttributeValue('drink', 'wine')
),
# Fraeport - whiskey
is_same_person(
AttributeValue('city', 'Fraeport'),
AttributeValue('drink', 'whiskey')
),
# center - absinthe
is_same_person(
AttributeValue('seat', 'center'),
AttributeValue('drink', 'absinthe')
),
# Baroness Finch - Karnaca
is_same_person(
AttributeValue('name', 'Baroness Finch'),
AttributeValue('city', 'Karnaca')
)
]
solutions = solve(attributes, conditions)
print 'Found {} solution(s):'.format(len(solutions))
print
for solution_index, solution in enumerate(solutions, 1):
print 'Solution {}:'.format(solution_index)
print
result = solution.result('name', 'heirloom')
for attribute_a_value in sorted(result.keys()):
print '{:16}: {:16}'.format(attribute_a_value, result[attribute_a_value])
print
if __name__ == '__main__':
sys.exit(main())