Skip to content

Instantly share code, notes, and snippets.

@SnowOnion
Last active March 17, 2020 04:12
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 SnowOnion/058b663d132da51f853118265a7069a4 to your computer and use it in GitHub Desktop.
Save SnowOnion/058b663d132da51f853118265a7069a4 to your computer and use it in GitHub Desktop.
WIP: Trying to answer https://adnmb.com/t/24354256
-- Trying to answer https://adnmb.com/t/24354256
-- Seems not correct. !!!!!!!!!
-- forfor :: Monad m =>
{-
[
[1,2,3],
[4,5],
[6,7]
]
[
[4,6],
[4,7],
[5,6],
[5,7]
]
-}
forfor :: [[a]] -> ([a] -> b) -> [b]
forfor candidates f = map f (enumAll candidates)
forforM :: Monad m => [[a]] -> ([a] -> m b) -> m [b]
forforM candidates f = mapM f (enumAll candidates)
enumAll :: [[a]] -> [[a]]
enumAll [] = [[]]
enumAll (xs:yss) = [ x:zs | x <- xs, zs <- enumAll yss]
candi = [
[1,2,3],
[4,5],
[6,7]
]
ff [x1,x2,x3] = 100 * x1 + 10 * x2 + x3
ff _ = error "mehhhhh"
ffm [x1,x2,x3] = print $ 100 * x1 + 10 * x2 + x3
ffm _ = error "mehhhhh"
-- main = do
-- print $ forfor candi ff
-- forforM candi ffm
(~>) :: Bool -> Bool -> Bool
p ~> q = not p || q
infixr 1 ~>
(<~>) :: Bool -> Bool -> Bool
(<~>) = (==)
infixr 1 <~>
p1 :: [Int] -> Bool
p1 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
x1 == x1
p2 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
x5 == (x2 + 2) `mod` 4
p3 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
(x3 == 1 ~> x3/=x6 && x3/=x2 && x3/=x4) &&
(x3 == 2 ~> x6/=x3 && x6/=x2 && x6/=x4) &&
(x3 == 3 ~> x2/=x3 && x2/=x6 && x2/=x4) &&
(x3 == 4 ~> x4/=x3 && x4/=x6 && x4/=x2)
p4 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
(x4 == 1 ~> x1==x5) &&
(x4 == 2 ~> x2==x7) &&
(x4 == 3 ~> x1==x9) &&
(x4 == 4 ~> x6==x10)
p5 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
(x5 == 1 ~> x8==x9) &&
(x5 == 2 ~> x1==x3) &&
(x5 == 3 ~> x4==x7) &&
(x5 == 4 ~> x2==x4)
p6 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
(x6 == 1 ~> x2==x4 && x4==x8) &&
(x6 == 2 ~> x1==x6 && x6==x8) &&
(x6 == 3 ~> x3==x10 && x10==x8) &&
(x6 == 4 ~> x5==x9 && x9==x8)
p7 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
(x7 == 1 ~> n3>n1 && n3>n2 && n3>n4) &&
(x7 == 2 ~> n2>n1 && n2>n3 && n2>n4) &&
(x7 == 3 ~> n1>n3 && n1>n2 && n1>n4) &&
(x7 == 4 ~> n4>n3 && n4>n1 && n4>n2)
where
n1 = sum [ 1 | x<-[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10], x==1]
n2 = sum [ 1 | x<-[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10], x==2]
n3 = sum [ 1 | x<-[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10], x==3]
n4 = sum [ 1 | x<-[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10], x==4]
p8 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
(x8 == 1 ~> abs (x1-x7) > 1) &&
(x8 == 2 ~> abs (x1-x5) > 1) &&
(x8 == 3 ~> abs (x1-x2) > 1) &&
(x8 == 4 ~> abs (x1-x10) > 1)
p9 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
(x9 == 1 ~> x1==x6 && x6==x5) &&
(x9 == 2 ~> x1==x10 && x10==x5) &&
(x9 == 3 ~> x1==x2 && x2==x5) &&
(x9 == 4 ~> x1==x9 && x9==x5)
p10 [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] =
(x10 == 1 ~> range==3) &&
(x10 == 2 ~> range==2) &&
(x10 == 3 ~> range==4) &&
(x10 == 4 ~> range==1)
where
range = maxn - minn
maxn = maximum [n1,n2,n3,n4]
minn = minimum [n1,n2,n3,n4]
n1 = sum [ 1 | x<-[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10], x==1]
n2 = sum [ 1 | x<-[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10], x==2]
n3 = sum [ 1 | x<-[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10], x==3]
n4 = sum [ 1 | x<-[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10], x==4]
ps = [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10]
-- map f [x1,x2,...] = [f x1, f x2, ...]
-- pam [f1,f2,...] x = [f1 x, f2 x, ...]
-- pamap [f1,f2,...] [x1,x2,...] = [f1 x1, f1 x2,..., f2 x1, f2 x2,...]
pamap :: [a -> b] -> [a] -> [b]
pamap = (<*>) -- ``built-in''! hahaha
-- Or simply use list comprehension.
multiChoiceCandidates = replicate 10 [1,2,3,4]
main = print $ length $ filter id $ pamap ps $ enumAll multiChoiceCandidates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment