Last active
October 9, 2015 05:42
-
-
Save xuwei-k/c77aa4e19c0b4d4c10e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scalaVersion := "2.11.7" | |
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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