Skip to content

Instantly share code, notes, and snippets.

@myeesan
Created January 16, 2015 13:16
Show Gist options
  • Save myeesan/bd97271a59420483cd8b to your computer and use it in GitHub Desktop.
Save myeesan/bd97271a59420483cd8b to your computer and use it in GitHub Desktop.
Rotate
object Rotate {
val line2 =
""".......
.......
.......
....R..
...RB..
..BRB..
.RBBR..""".lines.toList
val line =
"""ABCDEFG
HIJKLMN
OPQRSTU
VWXYZ01
...RB..
..BRB..
.RBBR..""".lines.toList
val K = 4
def main(args: Array[String]) {
val res = line.map(pushRight)
println(diags.toList)
}
def checkRow(line: String) = line.sliding(K).filter(_.toSet.size == 1)
def rows = line
def cols = {
val r = line(0).size
val res = for {
i <- 0 until r
} yield line.map(_(i)).mkString
res.toList
}
def diags = {
val r = 7
val res = for {
idx <- rd(r, K)
} yield line(idx._1)(idx._2)
res.grouped(K)
}
def rd(r: Int, k: Int) = {
val res = for {
i <- 0 until r
j <- 0 until r
if (i + k <= r && j + k <= r)
x <- 0 until k
val dx = i + x
val dy = j + x
} yield (dx, dy)
res.grouped(k).flatten
}
def pushRight(line: String): String = {
val r = line.size
val x = line.replace(".", "")
val empties = "." * (r - x.size)
empties + x
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment