Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active October 9, 2015 05:42
Show Gist options
  • Save xuwei-k/c77aa4e19c0b4d4c10e2 to your computer and use it in GitHub Desktop.
Save xuwei-k/c77aa4e19c0b4d4c10e2 to your computer and use it in GitHub Desktop.
scalaVersion := "2.11.7"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.4"
import scalaz._, Scalaz._
case class KnightPos(c: Int, r: Int) {
def move: List[KnightPos] =
for {
KnightPos(c2, r2) <- List(KnightPos(c + 2, r - 1), KnightPos(c + 2, r + 1),
KnightPos(c - 2, r - 1), KnightPos(c - 2, r + 1),
KnightPos(c + 1, r - 2), KnightPos(c + 1, r + 2),
KnightPos(c - 1, r - 2), KnightPos(c - 1, r + 2)) if (
((1 |-> 8) element c2) && ((1 |-> 8) contains r2))
} yield KnightPos(c2, r2)
val moveK: Kleisli[List, KnightPos, KnightPos] = Kleisli(_.move)
def in(n: Int): List[KnightPos] =
moveK.endo.multiply(n).run.run(this)
def canReachIn(n: Int, end: KnightPos): Boolean = in(n) contains end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment