Created
February 22, 2019 18:59
-
-
Save jfkelley/0e26d72153664738a1abefab1c2f4c86 to your computer and use it in GitHub Desktop.
Solves 538 Riddler Express for Feb 22 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
statements = [ | |
('a', 'b', lambda x: x > 20), | |
('b', 'c', lambda x: x > 18), | |
('c', 'd', lambda x: x < 22), | |
('d', 'e', lambda x: x != 17), | |
('e', 'a', lambda x: x > 21), | |
('a', 'd', lambda x: x > 16), | |
('b', 'e', lambda x: x < 20), | |
('c', 'a', lambda x: x == 19), | |
('d', 'b', lambda x: x == 20), | |
('e', 'c', lambda x: x < 18) | |
] | |
def check(ages): | |
for (speaker, subject, age_test) in statements: | |
if speaker in ages and subject in ages: | |
if (ages[speaker] >= l) == age_test(ages[subject]): | |
return False | |
return True | |
def search_ages(ages, unassigned): | |
if check(ages): | |
if len(unassigned) == 0: | |
print 'found solution, l={}, ages={}'.format(l, ages) | |
else: | |
for a in range(16, 23): | |
ages[unassigned[0]] = a | |
search_ages(ages, unassigned[1:]) | |
del ages[unassigned[0]] | |
for l in range(16, 23): | |
search_ages({}, ['a', 'b', 'c', 'd', 'e']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment