Skip to content

Instantly share code, notes, and snippets.

@t-katsushima
Created October 28, 2019 10:28
Show Gist options
  • Save t-katsushima/e87ea454615ca5ee222f7e1fb282bcc1 to your computer and use it in GitHub Desktop.
Save t-katsushima/e87ea454615ca5ee222f7e1fb282bcc1 to your computer and use it in GitHub Desktop.
15sisisin-solver
import java.util.Scanner
import scala.xml.XML
import scala.util.Random
/** div.boardを入力として与える **/
object Main extends App {
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" \ "div")
.map(row => (_ \ "@style").toString.split(",").last)
}
def coordToNumber(p: (Int, Int)): Int = {
val (x, y) = p
Math.abs(y / 80) * 4 + Math.abs(x / 80)
}
val board = Array.ofDim[Int](8, 8)
for (i <- 0 until 15) {
val (x, y) = (i % 4, i / 4)
}
for((x, y) <- parse(xml)) {
board(y)(x) = degToInt(deg)
}
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