Skip to content

Instantly share code, notes, and snippets.

@dramforever
Created September 8, 2014 06:57
Show Gist options
  • Save dramforever/e77f7bce0318a254ea77 to your computer and use it in GitHub Desktop.
Save dramforever/e77f7bce0318a254ea77 to your computer and use it in GitHub Desktop.
some qipa solver
import Data.List
import Data.Maybe
import Control.Monad
data Ans = A | B | C | D | Whatever deriving (Show, Eq, Ord)
ver ans 1 = (ans !! 1) == (ans !! 1)
ver ans 2 = Just (ans !! 5) == lookup (ans !! 2) [(A, C), (B, D), (C, A), (D, B)]
ver ans 3 = all (== other) others && this /= other
where other = head others
others = (ans !!) `map` otherNums
otherNums = (fromJust . flip lookup table) `map` ([A, B, C, D] \\ [ans !! 3])
table = [(A, 3), (B, 6), (C, 2), (D, 4)] :: [( Ans, Int )]
this = ans !! (fromJust $ lookup (ans !! 3) table)
ver ans 4 = fromJust $ lookup (ans !! 4) table
where table = [(A, e 1 5), (B, e 2 7), (C, e 1 9), (D, e 6 10)]
e a b = (ans !! a) == (ans !! b)
ver ans 5 = (ans !! 5) == (ans !! chosen)
where chosen = fromJust $ lookup (ans !! 5) table
table = [(A, 8), (B, 4), (C, 9), (D, 7)]
ver ans 6 = fromJust $ lookup (ans !! 6) table
where table = [(A, e 2 4), (B, e 1 6), (C, e 3 10), (D, e 5 9)]
e a b = (ans !! a) == (ans !! 8) && (ans !! b) == (ans !! 8)
ver ans 7 = (fromJust $ lookup (ans !! 7) table) == smallest
where table = [(A, C), (B, B), (C, A), (D, D)]
counts = sort . map (\x -> (length x, head x)) . group . sort . filter (/= Whatever) $ ans
smallest | not (A `elem` ans) = A
| not (B `elem` ans) = B
| not (C `elem` ans) = C
| not (D `elem` ans) = D
| otherwise = snd (head counts)
ver ans 8 = all adja' others && not (adja' this)
where adja' = adja (ans !! 1)
adja A B = True
adja A _ = False
adja B A = True
adja B C = True
adja B _ = False
adja C B = True
adja C D = True
adja C _ = False
adja D C = True
adja D _ = False
others = (ans !!) `map` otherNums
otherNums = (fromJust . flip lookup table) `map` ([A, B, C, D] \\ [ans !! 3])
table = [(A, 7), (B, 5), (C, 2), (D, 10)] :: [( Ans, Int )]
this = ans !! (fromJust $ lookup (ans !! 8) table)
ver ans 9 = (ans !! 1 == ans !! 6) /= (ans !! x == ans !! 5)
where x = fromJust $ lookup (ans !! 9) table
table = [(A, 6), (B, 10), (C, 2), (D, 9)]
ver ans 10 = (fromJust $ lookup (ans !! 10) table) == ma - mi
where table = [(A, 3), (B, 2), (C, 4), (D, 1)]
counts = map length . group . sort . filter (/= Whatever) $ ans
mi | not (A `elem` ans) = 0
| not (B `elem` ans) = 0
| not (C `elem` ans) = 0
| not (D `elem` ans) = 0
| otherwise = minimum counts
ma = maximum counts
verAll x = all id $ ver (Whatever : x) `map` [1..10]
tries = [ [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10] | a1 <- [A, B, C, D], a2 <- [A, B, C, D], a3 <- [A, B, C, D], a4 <- [A, B, C, D], a5 <- [A, B, C, D], a6 <- [A, B, C, D], a7 <- [A, B, C, D], a8 <- [A, B, C, D], a9 <- [A, B, C, D], a10 <- [A, B, C, D] ]
main = forM_ (filter verAll tries) print
[B,C,A,C,A,C,D,A,B,A]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment