-
-
Save myeesan/bd97271a59420483cd8b to your computer and use it in GitHub Desktop.
Rotate
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
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