Skip to content

Instantly share code, notes, and snippets.

@nicolasff
Created November 2, 2009 15:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicolasff/224222 to your computer and use it in GitHub Desktop.
Save nicolasff/224222 to your computer and use it in GitHub Desktop.
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