Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Created October 4, 2011 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halcat0x15a/1261213 to your computer and use it in GitHub Desktop.
Save halcat0x15a/1261213 to your computer and use it in GitHub Desktop.
Scalazの練習。GCJJのAを解いてみた。
import java.io._
import scala.io._
import scalaz._
import Scalaz._
import effects._
import IterV._
object A extends App {
val PairRegex = """(\d+) (\d+)""".r
val TripleRegex = """(\d+) (\d+) (\d+)""".r
val file = new File(args.head)
def cases(list: List[String]) = {
val triple = (list: List[String]) => list.collect {
case TripleRegex(m, c, w) => (m.toInt, c.toInt, w.toInt)
}
val pair = (list: List[String]) => {
val pairZero = mzero[(Int, Int)]
val pairs = list.map {
case PairRegex(a, b) => (a.toInt, b.toInt)
case _ => pairZero
}
pairs.selectSplit(pairZero /==)
}
(triple *** pair).apply(list.pair).fold(_ zip _).zipWithIndex.map {
case (((m, c, w), pairs), i) => {
val index = i |+| 1
val cards = pairs.foldl[IndexedSeq[Int]](1 to m) { (cards, pair) =>
pair match {
case (a, b) => cards.splitAt(a - 1) match {
case (x, tail) => tail.splitAt(b) match {
case (y, z) => y <+> x <+> z
}
}
}
}
val n = cards -!- (w - 1)
"Case #" |+| index.shows |+| ": " |+| n.shows
}
}
}
val main = for {
lines <- getFileLines(file)(drop(1) >|> collect[String, List]).map(_.run)
writer = new PrintWriter("A-small-practice.out")
_ <- io(writer).bracket(w => io(w.close))(w => cases(lines).traverse_(wPutStrLn(w, _)))
} yield ()
main.unsafePerformIO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment