Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
brute force solution in Clear category for The Einstein Problem-Lite by twilyght
import itertools
from collections import namedtuple
VALUES = {
'color': ['blue', 'green', 'red', 'white', 'yellow'],
'pet': ['cat', 'bird', 'dog', 'fish', 'horse'],
'beverage': ['beer', 'coffee', 'milk', 'tea', 'water'],
'cigarettes': ['Rothmans', 'Dunhill', 'Pall Mall', 'Winfield', 'Marlboro'],
'nationality': ['Brit', 'Dane', 'German', 'Norwegian', 'Swede'],
'number': ['1', '2', '3', '4', '5']
}
TYPES = {value: type for type, values in VALUES.items() for value in values}
def is_compatible(house, constraint):
res = all(getattr(house, c.attr) == c.value for c in constraint) \
or all(getattr(house, c.attr) != c.value for c in constraint)
return res
def answer(relations, question):
question_value, answer_type = question.split('-')
question_type = TYPES[question_value]
combs = list(itertools.product(*VALUES.values()))
House = namedtuple('House', VALUES.keys())
Predicate = namedtuple('Predicate', 'attr value')
houses = set(itertools.starmap(House, combs))
houses = {h for h in houses if getattr(h, question_type) == question_value}
for rel in relations:
s1, s2 = rel.split('-')
t1 = TYPES[s1]
t2 = TYPES[s2]
constraint = [Predicate(t1, s1), Predicate(t2, s2)]
fail_constraint = {h for h in houses if not is_compatible(h, constraint)}
houses -= fail_constraint
return getattr(houses.pop(), answer_type)
June 2, 2020
Comments: