Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Last active November 30, 2024 18:34
Show Gist options
  • Save mdwhatcott/9ca2cb81e431cbb3c71d52b60ba91bf1 to your computer and use it in GitHub Desktop.
Save mdwhatcott/9ca2cb81e431cbb3c71d52b60ba91bf1 to your computer and use it in GitHub Desktop.
"""
Solution to puzzle with ID C823GV from www.Printable-Puzzles.com
https://logic.puzzlebaron.com/pdf/C823GV.pdf
There are 4 named individuals: Caroline, Donovan, Keaton, and Lucy
There are 4 appointment times: 5:00pm, 5:30pm, 6:00pm, and 6:30pm
There are 4 origin cities: Seattle, Pittsburgh, Warwick, and Wilmington
There are 4 tv channels: CNN, HBO, Showtime, and Cinemax
The clues are as follows:
1. The person raised in Seattle is Lucy.
2. The person raised in Pittsburgh watched CNN.
3. The person who watched HBO has an earlier reservation than the person
raised in Warwick.
4. Either the person who watched HBO or the person who watched Showtime
is Donovan.
5. Lucy has a later reservation than the person who watched Cinemax.
6. Of Keaton and the person who watched HBO, one has the 6:00pm reservation
and the other has the 5:00pm reservation.
7. The 4 people were the person with a reservation at 6:30pm, Caroline, the
person raised in Wilmington, and the person who watched CNN.
There are over 13k permutations of the provided characteristics
amongst the 4 individuals named in the puzzle. Of the seven
hints above, only hint four is superfluous.
Definitely not the kind of thing I'd want to solve by hand.
This code produced with some aid from ChatGPT:
https://chatgpt.com/share/674a9be4-9244-800a-98df-253436659163
"""
from itertools import permutations
NAME = 0
CHAN = 1
CITY = 2
APPT = 3
def find(group, key, val):
for person in group:
if person[key] == val:
return person
def hint_one(group):
return find(group, NAME, "Lucy") == find(group, CITY, "Seattle")
def hint_two(group):
return find(group, CITY, "Pittsburgh") == find(group, CHAN, "CNN")
def hint_three(group):
return find(group, CHAN, "HBO")[APPT] < find(group, CITY, "Warwick")[APPT]
def hint_four(group):
hbo = find(group, CHAN, "HBO")
showtime = find(group, CHAN, "Showtime")
return hbo[NAME] == 'Donovan' or showtime[NAME] == 'Donovan'
def hint_five(group):
return find(group, NAME, "Lucy")[APPT] > find(group, CHAN, "Cinemax")[APPT]
def hint_six(group):
keaton = find(group, NAME, "Keaton")
hbo = find(group, CHAN, "HBO")
return set([keaton[APPT], hbo[APPT]]) == set([5, 6])
def hint_seven(group):
six_thirty = find(group, APPT, 6.5)[NAME]
caroline = find(group, NAME, "Caroline")[NAME]
wilmington = find(group, CITY, "Wilmington")[NAME]
cnn = find(group, CHAN, "CNN")[NAME]
return len(set([six_thirty, caroline, wilmington, cnn])) == 4
def valid(group):
return (
hint_one(group) and # 3456
hint_two(group) and # 864
hint_three(group) and # 288
hint_four(group) and # 120 # UNNECESARY!
hint_five(group) and # 16
hint_six(group) and # 4
hint_seven(group)) # 1
names = ["Caroline", "Lucy", "Donovan", "Keaton"]
channels = ["HBO", "CNN", "Cinemax", "Showtime"]
cities = ["Pittsburgh", "Seattle", "Warwick", "Wilmington"]
appointments = [5.0, 5.5, 6.0, 6.5]
def generate_groups():
for channel_perm in permutations(channels):
for city_perm in permutations(cities):
for appointment_perm in permutations(appointments):
group = [
[names[i],
channel_perm[i],
city_perm[i],
appointment_perm[i]]
for i in range(4)
]
yield group
total = 0
for group in generate_groups():
total += 1
if valid(group): # There should only be one valid group.
for person in group:
print(person)
print(f"Total unique groups: {total}") # 13824
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment