Created
November 2, 2009 15:32
-
-
Save nicolasff/224222 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.Writer | |
import System(getArgs, getProgName) | |
data Column = LeftCol | MiddleCol | RightCol | |
instance Show Column where | |
show LeftCol = "left" | |
show MiddleCol = "middle" | |
show RightCol = "right" | |
-- move a list of blocks from column x to column y with column z as temporary storage | |
move :: [Int] -> Column -> Column -> Column -> Writer String () | |
move [] _ _ _ = return () -- nothing to move. | |
move (largest:rest) src dst tmp = do | |
move rest src tmp dst -- move the top blocks from src to tmp using dest as temp | |
tell $ "move " ++ show largest ++ " from " ++ show src ++ " to " ++ show dst ++ "\n" -- move the largest one from src to dst | |
move rest tmp dst src -- move the tmp ones back onto the largest one on dst using src as temp | |
main :: IO () | |
main = do | |
args <- getArgs | |
if null args then do | |
getProgName >>= \argv0 -> putStrLn $ "Usage: " ++ argv0 ++ " <rings>" | |
else let n = read (head args) :: Int in do | |
putStrLn . snd . runWriter $ move [n,n-1..1] LeftCol RightCol MiddleCol |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment