Skip to content

Instantly share code, notes, and snippets.

@ognis1205
Last active August 29, 2015 14:28
Show Gist options
  • Save ognis1205/5afa9345e44196c163b8 to your computer and use it in GitHub Desktop.
Save ognis1205/5afa9345e44196c163b8 to your computer and use it in GitHub Desktop.
Scala Metaprogrammatic Hanoi's Tower Solver
object TowerOfHanoi {
import scala.reflect.Manifest
def name(manifest:Manifest[_]):String = {
manifest.erasure.getName
}
trait NaturalNumber
final class Zero extends NaturalNumber
final class Successor[Previous<:NaturalNumber] extends NaturalNumber
type One = Successor[Zero]
type Two = Successor[One]
type Three = Successor[Two]
type Four = Successor[Three]
type Five = Successor[Four]
type Six = Successor[Five]
type Seven = Successor[Six]
type Eight = Successor[Seven]
type Nine = Successor[Eight]
type Ten = Successor[Nine]
case class Move[N<:NaturalNumber, Peg1, Peg2, Peg3]()
implicit def move[Peg1, Peg2, Peg3](implicit peg1:Manifest[Peg1], peg2:Manifest[Peg2]):Move[Zero, Peg1, Peg2, Peg3] = {
System.out.println("Move from " + name(peg1) + " to " + name(peg2))
null
}
implicit def prepare[COUNT<:NaturalNumber, Peg1, Peg2, Peg3](
implicit move1:Move[COUNT, Peg1, Peg3, Peg2], move2:Move[ZERO, Peg1, Peg2, Peg3], move3:Move[COUNT, Peg3, Peg2, Peg1])
:Move[Successor[COUNT], Peg1, Peg2, Peg3] = null
def run[HEIGHT<:NaturalNumber, Peg1, Peg2, Peg3](implicit move:Move[HEIGHT, Peg1, Peg2, Peg3]) = null
case class Left()
case class Center()
case class Right()
def main(args:Array[String]){
run[Ten, Left, Right, Center]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment