module Problems.P91 (knightsTour, closedKnightsTour, printKnightsTour) where
import qualified Data.Map.Strict as Map
import qualified Solutions.P91 as Solution
knightsTour :: Int -> (Int,Int) -> Maybe [(Int,Int)]
knightsTour :: Int -> (Int, Int) -> Maybe [(Int, Int)]
knightsTour = Int -> (Int, Int) -> Maybe [(Int, Int)]
Solution.knightsTour
closedKnightsTour :: Int -> Maybe [(Int,Int)]
closedKnightsTour :: Int -> Maybe [(Int, Int)]
closedKnightsTour = Int -> Maybe [(Int, Int)]
Solution.closedKnightsTour
printKnightsTour :: Maybe [(Int,Int)] -> IO ()
printKnightsTour :: Maybe [(Int, Int)] -> IO ()
printKnightsTour Maybe [(Int, Int)]
Nothing = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
printKnightsTour (Just [(Int, Int)]
path) = (Int -> IO ()) -> [Int] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (String -> IO ()
putStrLn (String -> IO ()) -> (Int -> String) -> Int -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
line) [Int
1..Int
n]
where order :: Map (Int, Int) Int
order = [((Int, Int), Int)] -> Map (Int, Int) Int
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([((Int, Int), Int)] -> Map (Int, Int) Int)
-> [((Int, Int), Int)] -> Map (Int, Int) Int
forall a b. (a -> b) -> a -> b
$ [(Int, Int)] -> [Int] -> [((Int, Int), Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [(Int, Int)]
path [Int
1..(Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n)]
line :: Int -> String
line Int
y = [String] -> String
unwords ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Int -> String) -> [Int] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> String
forall {a}. Show a => a -> String
showInt (Int -> String) -> (Int -> Int) -> Int -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\Int
x -> Map (Int, Int) Int
order Map (Int, Int) Int -> (Int, Int) -> Int
forall k a. Ord k => Map k a -> k -> a
Map.! (Int
x,Int
y))) [Int
1..Int
n]
showInt :: a -> String
showInt a
k = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (a -> String
forall {a}. Show a => a -> String
show a
k)) Char
' ' String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall {a}. Show a => a -> String
show a
k
width :: Int
width = String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Int -> String
forall {a}. Show a => a -> String
show (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n)
l :: Int
l = [(Int, Int)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Int, Int)]
path
n :: Int
n = (Int -> Bool) -> (Int -> Int) -> Int -> Int
forall a. (a -> Bool) -> (a -> a) -> a -> a
until (\Int
k -> Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
1