Skip to content

Instantly share code, notes, and snippets.

@krrrr38
Created February 20, 2012 20:07
Show Gist options
  • Save krrrr38/1871092 to your computer and use it in GitHub Desktop.
Save krrrr38/1871092 to your computer and use it in GitHub Desktop.
それぞれ異なる色の、5つの建物が...
// http://d.hatena.ne.jp/kmizushima/20090629/1246212207
// 写経+α(関数少し変えただけ)
// 改行の練習になった気がする.
object Houses extends Enumeration{
val red = Value("Red")
val white = Value("White")
val green = Value("Green")
val yellow = Value("Yellow")
val blue = Value("Blue")
}
object Countries extends Enumeration{
val japan = Value("Japan")
val usa = Value("America")
val china = Value("China")
val brazil = Value("Brazil")
val italy = Value("Italy")
}
object Drinks extends Enumeration{
val milk = Value("Milk")
val tea = Value("Tea")
val coffee = Value("Coffee")
val water = Value("Water")
val beer = Value("Beer")
}
object Tabacoes extends Enumeration{
val mildseven = Value("MildSeven")
val sevenstar = Value("SevenStar")
val marlboro = Value("Marlboro")
val hope = Value("Hope")
val lark = Value("Lark")
}
object Pets extends Enumeration{
val cat = Value("Cat")
val dog = Value("Dog")
val bird = Value("Bird")
val horse = Value("Hourse")
val fish = Value("Fish")
}
case class Info(house: Houses.Value, country: Countries.Value, drink: Drinks.Value, tabaco: Tabacoes.Value, pet: Pets.Value)
// このc2って一体なんなんだ… 引数無しの関数でもないし,Booleanになるもの?
def -->(c1: Boolean, c2: => Boolean): Boolean = if (c1) c2 else true
def select[A](ls: List[A]): List[(A, List[A])] = ls map {n => (n, ls filterNot {_ == n})}
def forallWithIndex[A](arr: Array[A])(pred: (A, Int) => Boolean): Boolean = {
arr.zipWithIndex.forall(n => pred(n._1, n._2))
}
def solve(hs: List[Houses.Value], cs: List[Countries.Value], ds: List[Drinks.Value],
ts: List[Tabacoes.Value], ps: List[Pets.Value])(f: Array[Info] => Unit) = {
def solve_(hs: List[Houses.Value], cs: List[Countries.Value], ds: List[Drinks.Value],
ts: List[Tabacoes.Value], ps: List[Pets.Value], acc: List[Info]): Unit = {
hs match {
case _ :: _ => {
for{(h, hR) <- select(hs)
(c, cR) <- select(cs) if -->(c == Countries.japan, h == Houses.red)
(d, dR) <- select(ds) if -->(c == Countries.china, d == Drinks.tea)
if -->(h == Houses.green, d == Drinks.coffee)
(t, tR) <- select(ts) if -->(h == Houses.yellow, t == Tabacoes.mildseven)
if -->(t == Tabacoes.hope, d == Drinks.beer)
if -->(c == Countries.brazil, t == Tabacoes.lark)
(p, pR) <- select(ps) if -->(c == Countries.usa, p == Pets.dog)
if -->(t == Tabacoes.sevenstar, p == Pets.bird)
}{solve_(hR, cR, dR, tR, pR, Info(h,c,d,t,p) :: acc)
}
}
case Nil => {
val ps = acc.toArray
if (ps(0).country == Countries.italy
&& ps(2).drink == Drinks.milk
&& forallWithIndex(ps){(p, i) =>
( -->(i > 0 && p.house == Houses.white, ps(i-1).house == Houses.green)
&& -->(p.tabaco == Tabacoes.marlboro,
(i > 0 && ps(i-1).pet == Pets.cat) ||
(i < ps.length - 1 && ps(i+1).pet == Pets.cat))
&& -->(p.tabaco == Tabacoes.marlboro,
(i > 0 && ps(i-1).drink == Drinks.water) ||
(i < ps.length - 1 && ps(i+1).drink == Drinks.water))
&& -->(p.house == Houses.blue,
(i > 0 && ps(i-1).country == Countries.italy) ||
(i < ps.length - 1 && ps(i+1).country == Countries.italy))
&& -->(p.pet == Pets.horse,
(i > 0 && ps(i-1).tabaco == Tabacoes.mildseven) ||
(i < ps.length -1 && ps(i+1).tabaco == Tabacoes.mildseven)))
}
){ f(ps) }
}
}
}
solve_(hs,cs,ds,ts,ps,Nil)
}
lazy val output = {
solve(Houses.values.toList,
Countries.values.toList,
Drinks.values.toList,
Tabacoes.values.toList,
Pets.values.toList){ ps =>
println(ps.toList)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment