Create a gist now

Instantly share code, notes, and snippets.

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