Dice4-Scala

問題文
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_11_D

最近Scalaを学び始めた。詳しいことは後で。

import scala.io.StdIn._

object Main {
  case class Rotation(run: Dice => Dice){
    def apply(d: Dice): Dice = run(d)
    def compose(other: Rotation) = Rotation(run compose other.run)
  }

  object Rotation {
    def id = Rotation(d => d)
    def north = Rotation(d => Dice(List(1,5,2,3,0,4).map(d.pip)))
    def east = Rotation(d => Dice(List(3,1,0,5,4,2).map(d.pip)))
    def ccw = Rotation(d => Dice(List(0,3,1,4,2,5).map(d.pip)))

    final def axial(rot: Rotation): Seq[Rotation] =
      for(i <- 0 to 3) yield List.fill(i)(rot).fold(id)(_ compose _)

    def enumerateTop: Seq[Rotation] =
      for(i <- 1 to 6) yield ((0 to i).map(j => if(j % 2 == 0) north else east)).fold(id)(_ compose _)

    def enumerate: Seq[Rotation] = enumerateTop.flatMap(e => axial(ccw).map(e compose _))
  }

  case class Dice(pip: List[Int]){
    lazy val all: Seq[Dice] = Rotation.enumerate.map(_.run(this))
    def ===(that: Dice): Boolean = pip.corresponds(that.pip)(_ == _)
    def ==(that: Dice): Boolean = all.exists(_ === that)
  }

  def main(args: Array[String]): Unit = {
    val Array(n,_*) = readLine.split(" ").map(_.toInt)
    val dices = for(i <- 1 to n) yield Dice(readLine.split(" ").map(_.toInt).toList)

    val res = for {
      i <- 0 until n
      j <- i+1 until n
    } yield dices(i) == dices(j)

    println(if(res.forall(_ == false)) "Yes" else "No")
  }
}