Towers of Hanoi from a Random Start

  1. You may only move one disk at a time, and must place that disk onto a tower before you can pick up a new one.
  2. You may never place a larger disk on top of a smaller disk.
  1. If the largest disk is already on the right tower, ignore it and start these instructions over considering only the smaller disks.
  2. Run this same algorithm to move the smaller disks to the auxiliary tower.
  3. Move the largest disk to the target tower.
  4. Run this same algorithm to move the smaller disks to the target tower.
solve = go 3
where
go _ [] = []
go i (n : ns)
| n /= i = clear ++ [(1, i)] ++ stack
| otherwise = first (+ 1) <$> go i ns
where
aux = 6 - n - i
clear = first (+ 1) <$> go aux ns
stack = first (+ 1) <$> go i ((const aux) <$> ns)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chris Smith

Chris Smith

Software engineer, volunteer K-12 math and computer science teacher, author of the CodeWorld platform, amateur ring theorist, and Haskell enthusiast.