Created
December 13, 2016 21:32
-
-
Save lonnelars/eb73971dc7a3300620c3892c8adaae30 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
import javaslang.Function1; | |
import javaslang.Function2; | |
import javaslang.Tuple; | |
import javaslang.Tuple2; | |
import javaslang.collection.List; | |
public class Knight { | |
static final Function1<Tuple2<Integer, Integer>, List<Tuple2<Integer, Integer>>> moveKnight = (start) -> { | |
// c,r = column,row | |
int c = start._1; | |
int r = start._2; | |
return List.of( | |
Tuple.of(c + 2, r - 1), | |
Tuple.of(c + 2, r + 1), | |
Tuple.of(c - 2, r - 1), | |
Tuple.of(c - 2, r + 1), | |
Tuple.of(c + 1, r - 2), | |
Tuple.of(c + 1, r + 2), | |
Tuple.of(c - 1, r - 2), | |
Tuple.of(c - 1, r + 2) | |
) | |
.filter( | |
t -> t._1 > 0 && t._1 < 9 && t._2 > 0 && t._2 < 9 | |
); | |
}; | |
static final Function1<Tuple2<Integer, Integer>, List<Tuple2<Integer, Integer>>> in3 = | |
(start) -> List.of(start).flatMap(moveKnight).flatMap(moveKnight).flatMap(moveKnight); | |
static final Function2<Tuple2<Integer, Integer>, Tuple2<Integer, Integer>, Boolean> canReachIn3 = | |
(start, end) -> in3.apply(start).contains(end); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment