Created
September 12, 2010 13:46
-
-
Save papamitra/576123 to your computer and use it in GitHub Desktop.
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
// Scalaの限定継続で四天王問題解いてみた その2 | |
// http://q.hatena.ne.jp/1284217294 | |
// scalac -P:continuations:enable Amb2.scala | |
import scala.util.continuations._ | |
object Amb{ | |
def amb[T](lst:T*):T @ cpsParam[Unit,Unit] = shift{k:(T=>Unit) => lst.foreach(k)} | |
def require(p:Boolean):Unit @ cpsParam[Unit,Unit] = shift{k:(Unit=>Unit) => if(p)k()} | |
def distinct(lst:Int*)= { | |
def proc(l:List[Int]):Boolean = l match{ | |
case Nil => true | |
case x::xs => (xs.indexOf(x)<0) && proc(xs) | |
} | |
proc(lst.toList) | |
} | |
def main(args:Array[String]){ | |
reset{ | |
val a,b,c,d = amb(1,2,3,4) | |
val at,bt,ct,dt = amb(true, false) | |
// 同じ強さでは無いことを仮定 | |
require(distinct(a,b,c,d)) | |
// A「Dがやられたようだな…」B「ククク…奴は我ら四天王の中でも最弱…」 | |
require((bt && d==4) || | |
(!bt && d!=4)) | |
// C「私はBよりも強い」 | |
require((ct && c < b) || | |
(!ct && b < c)) | |
// A「私は最強ではないが最弱でもない」 | |
require((at && (a != 1 && a != 4)) || | |
(!at && (a == 1 || a == 4))) | |
// B「私はAより強いぞ」 | |
require((bt && b < a) || | |
(!bt && a < b)) | |
// C「四天王NO.3は嘘つき」 | |
require(c != 3) | |
require((ct && ((!at && a==3) || (!bt && b==3) || (!dt && d==3))) || | |
(!ct && ((at && a==3) || (bt && b==3) || (dt && d==3)))) | |
// A「私とCとの実力差は離れている」 | |
// 順位が隣合っていないと解釈する. | |
import scala.math._ | |
require((at && (abs(a-c) != 1)) || | |
(!at && (abs(a-c) == 1))) | |
// ※以上の条件から四天王を強い順に並べよ | |
println(("abcd" zip List(a,b,c,d)).toList.sortWith(_._2 < _._2)) | |
println("abcd" zip List(at,bt,ct,dt)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment