Skip to content

Instantly share code, notes, and snippets.

@dktn
Created March 14, 2019 00:53
Show Gist options
  • Save dktn/4c032b3468ae22f1dcdc38903358e836 to your computer and use it in GitHub Desktop.
Save dktn/4c032b3468ae22f1dcdc38903358e836 to your computer and use it in GitHub Desktop.
Brute force solver 1
module Solver1 where
import Data.Ord (comparing)
import Data.Foldable (minimumBy)
maxVal :: [(Int, Int)] -> Int
maxVal l = max (maximum l1) (maximum l2)
where
(l1, l2) = unzip l
best :: [[(Int, Int)]] -> [(Int, Int)]
best = minimumBy $ comparing maxVal
gen :: Int -> Int -> (Int, Int) -> (Int, Int) -> [(Int, Int)]
gen 0 _ _ __ = []
gen n m p@(ap, bp) (an, bn) = best $ assign <$> els
where
els = if n == 1
then [(a, b) | a <- [1..m], b <- [1..m], a /= b, a /= an, b /= bn, a /= bn, b /= an, a /= ap, b /= bp, a /= bp, b /= ap]
else [(a, b) | a <- [1..m], b <- [1..m], a /= b, a /= an, b /= bn, a /= bn, b /= an]
assign e = e : gen (n - 1) m p e
solve :: Int -> Int -> [(Int, Int)]
solve n m = best $ assign <$> els
where
els = [(a, b) | a <- [1..m], b <- [1..m], a /= b]
assign e = e : gen (n - 1) m e e
main :: IO ()
main = do
putStrLn $ "result for 4: " ++ show (solve 4 6)
putStrLn $ "result for 5: " ++ show (solve 5 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment