Created
March 12, 2010 22:28
-
-
Save deanwampler/330877 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
case class Point(x: Int, y: Int) { | |
require(x >= 0) | |
require(y >= 0) | |
} | |
case class Piece(ps: Set[Point]) { | |
def flip = { | |
val mx = spanx | |
Piece(ps.map(p => Point(mx - p.x, p.y))) | |
} | |
def rotate = { | |
val my = spany | |
Piece(ps.map(p => Point(my - p.y, p.x))) | |
} | |
def permute = { | |
import scala.collection.mutable.{Set => MutableSet} | |
def loop(acc: MutableSet[Piece], p: Piece) { | |
if (!acc.contains(p)) | |
loop(acc += p, p.rotate) | |
} | |
val acc = MutableSet[Piece]() | |
loop(acc, this) | |
loop(acc, this.flip) | |
acc.toSet | |
} | |
def spanx = ps.map(_.x).max | |
def spany = ps.map(_.y).max | |
def dump { | |
val chrs = Map(true -> "*", false -> " ") | |
val mx = spanx | |
val my = spany | |
for (y <- 0 to my) { | |
val xs = ps.filter(_.y == y).map(_.x) | |
println("[" + (0 to mx).map(x => chrs(xs(x))).mkString + "]") | |
} | |
println | |
} | |
} | |
object Piece { | |
def fromCoords(coords: (Int, Int)*) = | |
Piece(coords.map(t => Point(t._1, t._2)).toSet) | |
def all = List( | |
// 1 | |
Piece.fromCoords((0, 0)), | |
// 2 | |
Piece.fromCoords((0, 0), (0, 1)), | |
// 3 | |
Piece.fromCoords((0, 0), (0, 1), (0, 2)), | |
Piece.fromCoords((0, 0), (0, 1), (1, 1)), | |
// 4 | |
Piece.fromCoords((0, 0), (0, 1), (0, 2), (0, 3)), | |
Piece.fromCoords((0, 2), (1, 0), (1, 1), (1, 2)), | |
Piece.fromCoords((0, 0), (0, 1), (0, 2), (1, 1)), | |
Piece.fromCoords((0, 0), (0, 1), (1, 0), (1, 1)), | |
Piece.fromCoords((0, 0), (1, 0), (1, 1), (2, 1)), | |
// 5 | |
Piece.fromCoords((0, 0), (0, 1), (0, 2), (0, 3), (0, 4)), | |
Piece.fromCoords((0, 3), (1, 0), (1, 1), (1, 2), (1, 3)), | |
Piece.fromCoords((0, 2), (0, 3), (1, 0), (1, 1), (1, 2)), | |
Piece.fromCoords((0, 1), (0, 2), (1, 0), (1, 1), (1, 2)), | |
Piece.fromCoords((0, 0), (0, 2), (1, 0), (1, 1), (1, 2)), | |
Piece.fromCoords((0, 0), (0, 1), (0, 2), (0, 3), (1, 1)), | |
Piece.fromCoords((0, 2), (1, 0), (1, 1), (1, 2), (2, 2)), | |
Piece.fromCoords((0, 0), (0, 1), (0, 2), (1, 2), (2, 2)), | |
Piece.fromCoords((0, 0), (1, 0), (1, 1), (2, 1), (2, 2)), | |
Piece.fromCoords((0, 0), (0, 1), (1, 1), (2, 1), (2, 2)), | |
Piece.fromCoords((0, 0), (0, 1), (1, 1), (2, 1), (1, 2)), | |
Piece.fromCoords((0, 1), (1, 0), (1, 1), (1, 2), (2, 1)) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment