June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -8,22 +8,8 @@
|
|||
*/
|
||||
|
||||
object Einstein extends App {
|
||||
class House(val nationality: String, val color: String, val beverage: String, val animal: String, val brand: String) {
|
||||
override def toString = { f"$nationality%10s, ${color + ", "}%-8s$beverage,\t$animal,\t$brand." }
|
||||
|
||||
def totalUnEqual(home2: House) =
|
||||
this.animal != home2.animal &&
|
||||
this.beverage != home2.beverage &&
|
||||
this.brand != home2.brand &&
|
||||
this.color != home2.color &&
|
||||
this.nationality != home2.nationality
|
||||
|
||||
//** Checks if the this green house is next to the other white house*/
|
||||
def checkAdjacentWhite(home2: House) = (this.color == "Green") == (home2.color == "White") // #5
|
||||
}
|
||||
|
||||
val possibleMembers = for { // pair clues results in 78 members
|
||||
nationality <- List("Norweigan", "German", "Dane", "Englishman", "Swede")
|
||||
nationality <- List("Norwegian", "German", "Dane", "Englishman", "Swede")
|
||||
color <- List("Red", "Green", "Yellow", "White", "Blue")
|
||||
beverage <- List("Milk", "Coffee", "Tea", "Beer", "Water")
|
||||
animal <- List("Dog", "Horse", "Birds", "Cats", "Zebra")
|
||||
|
|
@ -37,6 +23,22 @@ object Einstein extends App {
|
|||
if (brand == "Blue Master") == (beverage == "Beer") // #13
|
||||
if (brand == "Prince") == (nationality == "German") // #14
|
||||
} yield new House(nationality, color, beverage, animal, brand)
|
||||
val members = for { // Neighborhood clues
|
||||
h1 <- housesLeftOver().filter(p => (p.nationality == "Norwegia" /* #10 */) && (p.color != "Green") /* #5a */) // 28
|
||||
h3 <- housesLeftOver(h1).filter(p => p.beverage == "Milk") // #9 // 24
|
||||
h2 <- housesLeftOver(h1, h3).filter(_.color == "Blue") // #15
|
||||
if matchMiddleBrandAnimal(h1, h2, h3, "Blend", "Cats") // #11
|
||||
if matchCornerBrandAnimal(h1, h2, "Horse", "Dunhill") // #12
|
||||
h4 <- housesLeftOver(h1, h2, h3).filter(_.checkAdjacentWhite(h3) /* #5 */)
|
||||
h5 <- housesLeftOver(h1, h2, h3, h4)
|
||||
|
||||
// Redundant tests
|
||||
if h2.checkAdjacentWhite(h1)
|
||||
if h3.checkAdjacentWhite(h2)
|
||||
if matchCornerBrandAnimal(h5, h4, "Horse", "Dunhill")
|
||||
if matchMiddleBrandAnimal(h2, h3, h4, "Blend", "Cats")
|
||||
if matchMiddleBrandAnimal(h3, h4, h5, "Blend", "Cats")
|
||||
} yield Seq(h1, h2, h3, h4, h5)
|
||||
|
||||
def matchMiddleBrandAnimal(home1: House, home2: House, home3: House, brand: String, animal: String) =
|
||||
(home1.animal == animal || home2.brand != brand || home3.animal == animal) &&
|
||||
|
|
@ -49,28 +51,28 @@ object Einstein extends App {
|
|||
possibleMembers.filter(house => pickedHouses.forall(_.totalUnEqual(house)))
|
||||
}
|
||||
|
||||
val members = for { // Neighborhood clues
|
||||
h1 <- housesLeftOver().filter(p => (p.nationality == "Norweigan" /* #10 */ ) && (p.color != "Green") /* #5a */ ) // 28
|
||||
h3 <- housesLeftOver(h1).filter(p => p.beverage == "Milk") // #9 // 24
|
||||
h2 <- housesLeftOver(h1, h3).filter(_.color == "Blue") // #15
|
||||
if matchMiddleBrandAnimal(h1, h2, h3, "Blend", "Cats") // #11
|
||||
if matchCornerBrandAnimal(h1, h2, "Horse", "Dunhill") // #12
|
||||
h4 <- housesLeftOver(h1, h2, h3).filter(_.checkAdjacentWhite(h3) /* #5 */ )
|
||||
h5 <- housesLeftOver(h1, h2, h3, h4)
|
||||
class House(val nationality: String, val color: String, val beverage: String, val animal: String, val brand: String) {
|
||||
override def toString = {
|
||||
f"$nationality%10s, ${color + ", "}%-8s$beverage,\t$animal,\t$brand."
|
||||
}
|
||||
|
||||
// Redundant tests
|
||||
if h2.checkAdjacentWhite(h1)
|
||||
if h3.checkAdjacentWhite(h2)
|
||||
if matchCornerBrandAnimal(h5, h4, "Horse", "Dunhill")
|
||||
if matchMiddleBrandAnimal(h2, h3, h4, "Blend", "Cats")
|
||||
if matchMiddleBrandAnimal(h3, h4, h5, "Blend", "Cats")
|
||||
} yield Seq(h1, h2, h3, h4, h5)
|
||||
def totalUnEqual(home2: House) =
|
||||
this.animal != home2.animal &&
|
||||
this.beverage != home2.beverage &&
|
||||
this.brand != home2.brand &&
|
||||
this.color != home2.color &&
|
||||
this.nationality != home2.nationality
|
||||
|
||||
// Main program
|
||||
val beest = "Zebra"
|
||||
members.flatMap(p => p.filter(p => p.animal == beest)).
|
||||
foreach(s => println(s"The ${s.nationality} is the owner of the ${beest.toLowerCase}."))
|
||||
//** Checks if the this green house is next to the other white house*/
|
||||
def checkAdjacentWhite(home2: House) = (this.color == "Green") == (home2.color == "White") // #5
|
||||
}
|
||||
|
||||
println(s"The ${members.size} solution(s) are:")
|
||||
members.foreach(solution => solution.zipWithIndex.foreach(h => println("House " + (h._2 + 1) + " " + h._1)))
|
||||
}
|
||||
{ // Main program
|
||||
val beest = "Zebra"
|
||||
members.flatMap(p => p.filter(p => p.animal == beest)).
|
||||
foreach(s => println(s"The ${s.nationality} is the owner of the ${beest.toLowerCase}."))
|
||||
|
||||
println(s"The ${members.size} solution(s) are:")
|
||||
members.foreach(solution => solution.zipWithIndex.foreach(h => println(s"House ${h._2 + 1} ${h._1}")))
|
||||
}
|
||||
} // loc 58
|
||||
|
|
|
|||
|
|
@ -78,8 +78,7 @@ object Einstein extends App {
|
|||
// print each row including a column header
|
||||
((1 to 5).map(n => s"House $n") +: solution).map(_.map(pretty)).map(x => (pretty(labels.next) +: x).mkString(" ")).foreach(println)
|
||||
|
||||
println()
|
||||
println(s"The ${solution(1)(solution(3).indexOf("Fish"))} owns the Fish")
|
||||
println(s"\nThe ${solution(1)(solution(3).indexOf("Fish"))} owns the Fish")
|
||||
}
|
||||
|
||||
}
|
||||
}// loc 38
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue