Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active November 19, 2019 13:09
Show Gist options
  • Save lovasoa/f2b4ed93e755bf4172583d28f20635a3 to your computer and use it in GitHub Desktop.
Save lovasoa/f2b4ed93e755bf4172583d28f20635a3 to your computer and use it in GitHub Desktop.
-- Run online at: https://repl.it/repls/WeeSlightExams
-- hackernews thread: https://news.ycombinator.com/item?id=21543377
solve funcs = filter (matches funcs) (possibilities funcs)
matches funcs truths = truths == zipWith (($).($truths).flip) funcs [0..]
possibilities [] = [[]]
possibilities (x:xs) = concatMap (sequence [(True:), (False:)]) (possibilities xs)
all_of = ((and.).)
one_of = ((or.).)
none_of = (((not.or).).)
the_above = take
the_below = drop.(+1)
main = mapM_ putStrLn $
zipWith
(\i s -> "Answer number " ++ (show i) ++ " is " ++ (show s))
[1..] $
head $ solve [
all_of the_below,
none_of the_below,
all_of the_above,
one_of the_above,
none_of the_above,
none_of the_above
]
import itertools
# This solves the following question:
# https://math.stackexchange.com/questions/2217248/which-answer-in-this-list-is-the-correct-answer-to-this-question
# Run online at: https://repl.it/repls/CourteousCreepyAdaware
# The code tests all possible combinations of truth values for each of the 6 answers
# For each combination, it computes a tuple containing the truth values of the 6 propositions
# and checks that it equals the current combination
print([
q for q in itertools.product((True, False), repeat=6)
if q == (
all(q[1:]), # 1. All of the below.
not any(q[2:]), # 2. None of the below.
all(q[:2]), # 3. All of the above.
any(q[:3]), # 4. One of the above.
not any(q[:4]), # 5. None of the above.
not any(q[:5]), # 6. None of the above.
)
])
@madnight
Copy link

madnight commented Nov 16, 2019

Prolog

solution([A1,A2,A3,A4,A5,A6]) :- 
    sat(A1 =:= A2*A3*A4*A5*A6),
    sat(A2 =:= ~(A3+A4+A5+A6)),
    sat(A3 =:= A1*A2), 
    sat(A4 =:= A1+A2+A3), 
    sat(A5 =:= ~(A1+A2+A3+A4)),
    sat(A6 =:= ~(A1+A2+A3+A4+A5)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment