Skip to content

Instantly share code, notes, and snippets.

@t-katsushima
Created June 30, 2018 09:11
Show Gist options
  • Save t-katsushima/712ff34cc30080e19770681e0f7a5f59 to your computer and use it in GitHub Desktop.
Save t-katsushima/712ff34cc30080e19770681e0f7a5f59 to your computer and use it in GitHub Desktop.
import java.util.Scanner
import scala.xml.XML
import scala.util.Random
/** div.boardを入力として与える **/
object Main extends App {
val answer = Array(
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
)
// val answer = Array(
// "22222022",
// "22222022",
// "00022022",
// "22222222",
// "22222222",
// "22022000",
// "22022222",
// "22022222",
// )
val sc = new Scanner(System.in)
val xmlString = sc.nextLine()
val xml = XML.loadString(xmlString)
def parse(a: scala.xml.Elem): Seq[(Int, Int, Int)] = { // (x, y, deg)
def getByTag(node: scala.xml.NodeSeq, tag: String): Int = (node \ tag).toString.toInt
(a \ "div")
.map(row => (getByTag(row, "@data-x"), getByTag(row, "@data-y"), getByTag(row, "@data-deg")))
}
def degToInt(deg: Int): Int = (deg % 360) / 90
val board = Array.ofDim[Int](8, 8)
for((x, y, deg) <- parse(xml)) {
board(y)(x) = degToInt(deg)
}
def copyBoard(): Array[Array[Int]] = {
val res = Array.ofDim[Int](8, 8)
for(y <- 0 until 8; x <- 0 until 8)
res(y)(x) = board(y)(x)
res
}
var operation: List[(Int, Int, Int)] = Nil // (x, y, clickNum)
def make3dir(x: Int, y: Int): List[(Int, Int)] = {
def inRange(p: (Int, Int)) = {
val (x, y) = p
0 <= x && x < 8 && 0 <= y && y < 8
}
List((x-1, y), (x, y+1), (x+1, y)).filter(inRange)
}
def update(x: Int, y: Int, clickNum: Int, memo: Array[Array[Int]]): Unit = {
memo(y)(x) = (memo(y)(x) + clickNum) % 4
make3dir(x, y).map { case (x, y) => memo(y)(x) = (memo(y)(x) - clickNum) % 4 }
}
def calc(lst: List[Int]): Unit = {
var res: List[(Int, Int, Int)] = Nil
val memo = copyBoard()
for (x <- 0 until 8) {
val clickNum = lst(x)
res = (x, 0, clickNum) :: res
update(x, 0, clickNum, memo)
}
for(y <- 1 until 8; x <- 0 until 8) {
val upNum = memo(y-1)(x)
val clickNum = {
val ans = answer(y-1)(x).toString.toInt
val tmp = upNum - ans
if(tmp >= 0)
tmp
else
4 + (tmp % 4)
}
res = (x, y, clickNum) :: res
update(x, y, clickNum, memo)
}
if(memo(7).toList.map(x => if(x < 0) 4 + x else x) == answer(7).toList.map(_.toString.toInt)) {
if(operation.isEmpty)
operation = res
}
}
def solve(acc: List[Int]): Unit = {
if(!operation.isEmpty)
()
else if (acc.length == 8) {
calc(acc)
}
else {
for(n <- 0 to 3)
solve(n :: acc)
}
}
solve(Nil)
if(operation.isEmpty)
throw new Exception("おしめえだ!")
else {
var timeCnt = 0
val ansList = operation.toList.collect {
case (x, y, clickNum) if clickNum > 0 =>
List.fill(clickNum)((x, y))
.map { case (x, y) => s"$$(document).trigger('si:click', [$x,$y])" }
.mkString(";")
}
val ans = Random.shuffle(ansList)
.map{ s => timeCnt += 1; s"setTimeout(() => {$s}, ${timeCnt * 50})" }
.mkString(";")
println(ans)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment