Skip to content

Instantly share code, notes, and snippets.

@luochen1990
Created February 14, 2022 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luochen1990/8be1059e1871139023f738a4b1c4401a to your computer and use it in GitHub Desktop.
Save luochen1990/8be1059e1871139023f738a4b1c4401a to your computer and use it in GitHub Desktop.
logic puzzle
#!/usr/bin/env nix-shell
#! nix-shell -p "ghc"
#! nix-shell -i "runghc"
-- https://www.zhihu.com/question/23999095/answer/2254625328
import Control.Monad (guard)
brain_states :: (Int, Int) -> [Int]
brain_states (x, y) = [z | z <- [x + y, abs(x - y)], z > 0]
unsure :: [a] -> Bool
unsure xs = length xs > 1
solution = do
x <- [1..200]
y <- [1..200]
z <- [144]
let info1 = \(x, y, z) -> unsure (brain_states (y, z))
let info2 = \(x, y, z) -> unsure ([y | y <- brain_states (x, z), info1 (x, y, z)])
let info3 = \(x, y, z) -> unsure ([z | z <- brain_states (x, y), info1 (x, y, z), info2 (x, y, z)])
let info4 = \(x, y, z) -> unsure ([x | x <- brain_states (y, z), info2 (x, y, z), info3 (x, y, z)])
let info5 = \(x, y, z) -> unsure ([y | y <- brain_states (x, z), info1 (x, y, z), info3 (x, y, z), info4 (x, y, z)])
let info6 = \(x, y, z) -> ([z | z <- brain_states (x, y), info1 (x, y, z), info2 (x, y, z), info4 (x, y, z), info5 (x, y, z)]) == [144]
guard $ info1 (x, y, z)
guard $ info2 (x, y, z)
guard $ info3 (x, y, z)
guard $ info4 (x, y, z)
guard $ info5 (x, y, z)
guard $ info6 (x, y, z)
pure (x, y, z)
main = print solution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment