Avatar of Scuilion

Scuilion's solution

to Zebra Puzzle in the Scala Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Solve the zebra puzzle.

  1. There are five houses.
  2. The Englishman lives in the red house.
  3. The Spaniard owns the dog.
  4. Coffee is drunk in the green house.
  5. The Ukrainian drinks tea.
  6. The green house is immediately to the right of the ivory house.
  7. The Old Gold smoker owns snails.
  8. Kools are smoked in the yellow house.
  9. Milk is drunk in the middle house.
  10. The Norwegian lives in the first house.
  11. The man who smokes Chesterfields lives in the house next to the man with the fox.
  12. Kools are smoked in the house next to the house where the horse is kept.
  13. The Lucky Strike smoker drinks orange juice.
  14. The Japanese smokes Parliaments.
  15. The Norwegian lives next to the blue house.

Each of the five houses is painted a different color, and their inhabitants are of different national extractions, own different pets, drink different beverages and smoke different brands of cigarettes.

Which of the residents drinks water? Who owns the zebra?

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

Wikipedia https://en.wikipedia.org/wiki/Zebra_Puzzle

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ZebraPuzzleTest.scala

import org.scalatest.{FunSuite, Matchers}

import ZebraPuzzle._

/** @version created manually **/
class ZebraPuzzleTest extends FunSuite with Matchers {
  test("solve the Zebra Puzzle") {
    ZebraPuzzle.solve should be (
        Solution(waterDrinker = Norwegian,
                 zebraOwner = Japanese))
  }
}
object ZebraPuzzle {

  import scala.util.Try

  sealed trait Resident
  case object Englishman extends Resident
  case object Spaniard extends Resident
  case object Ukrainian extends Resident
  case object Norwegian extends Resident
  case object Japanese extends Resident

  case class Solution(waterDrinker: Resident, zebraOwner: Resident)


  val COLS = List("Red", "Green", "Yellow", "Ivory", "Blue")
  val NATS = List(Englishman, Spaniard, Ukrainian, Norwegian, Japanese)
  val BEVS = List("Milk", "Coffee", "Tea", "Orange Juice", "Water")
  val ANIS = List("Dog", "Horse", "Snails", "Fox", "Zebra")
  val CIGS = List("Kools", "Chesterfields", "Lucky Strike", "Parliaments", "Old Gold")

  // 6. The green house is immediately to the right of the ivory house.
  def colorRules(c: Seq[String]): Boolean = {
    Try(c(c.indexOf("Green") + 1) == "Ivory").getOrElse(false)
  }

  // 10. The Norwegian lives in the first house.
  // 2. The Englishman lives in the red house.
  // 15. The Norwegian lives next to the blue house.
  def nationalityRules(n: Seq[Resident], c: Seq[String]): Boolean = {
    val englishman = n.indexOf(Englishman)
    val norwegian = n.indexOf(Norwegian)
    val blueHouse = c.indexOf("Blue")

    (n(0) == Norwegian && c(englishman) == "Red"
      && (Try(c(norwegian - 1) == "Blue").getOrElse(false)
        || Try(c(norwegian + 1) == "Blue").getOrElse(false)))
  }

  // beverage                     9. Milk is drunk in the middle house.
  // color,         beverage      4. Coffee is drunk in the green house.
  // nationality,   beverage      5. The Ukrainian drinks tea.
  def beverageRules(b: Seq[String], n: Seq[Resident], c: Seq[String]): Boolean = {
    val greenHouse = c.indexOf("Green")
    (b(2) == "Milk" && b(greenHouse) == "Coffee" && n.indexOf(Ukrainian) == b.indexOf("Tea"))
  }

  // nationality,   animal        3. The Spaniard owns the dog.
  def animalRules(a: Seq[String], n: Seq[Resident]): Boolean = {
    (a.indexOf("Dog") == n.indexOf(Spaniard))
  }

  // animal,        cigs          7. The Old Gold smoker owns snails.
  // animal,        cigs          11. The man who smokes Chesterfields lives in the house next to the man with the fox.
  // animal,        cigs          12. Kools are smoked in the house next to the house where the horse is kept.
  // color,         cigs          8. Kools are smoked in the yellow house.
  // beverage,      cigs          13. The Lucky Strike smoker drinks orange juice.
  // nationality,   cigs          14. The Japanese smokes Parliaments.
  def cigaretteRules(cigs: Seq[String], colors: Seq[String], nats: Seq[Resident], bevs: Seq[String], anis: Seq[String]): Boolean = {
    val snails = anis.indexOf("Snails")
    val fox = anis.indexOf("Fox")
    val horse = anis.indexOf("Horse")

    val chester = cigs.indexOf("Chesterfields")
    val kools = cigs.indexOf("Kools")
    val lucky = cigs.indexOf("Lucky Strike")
    val parliaments = cigs.indexOf("Parliaments")

    val yellow = colors.indexOf("Yellow")

    val oj = bevs.indexOf("Orange Juice")

    val jap = nats.indexOf(Japanese)

    (cigs.indexOf("Old Gold") == anis.indexOf("Snails")
      && (fox - 1 == chester || fox + 1 == chester)
      && (horse - 1 == kools || horse + 1 == kools)
      && kools == yellow
      && lucky == oj
      && jap == parliaments)
  }

  //def whole() = {
  lazy val solve: Solution = {
    val solution = for {
      colors <- COLS.permutations if colorRules(colors)
      nats <- NATS.permutations if nationalityRules(nats, colors)
      beverages <- BEVS.permutations if beverageRules(beverages, nats, colors)
      animals <- ANIS.permutations if animalRules(animals, nats)
      cigs <- CIGS.permutations if cigaretteRules(cigs, colors, nats, beverages, animals)
    } yield new Answer(colors, nats, beverages, animals, cigs)

    (solution.map{ s =>
      val waterHouse = s.beverages.indexOf("Water")
      val zebraHouse = s.animals.indexOf("Zebra")
      Solution(s.nationalities(waterHouse),s.nationalities(zebraHouse))
    }).next
  }

  class Answer(
    val colors:        List[String],
    val nationalities: List[Resident],
    val beverages:     List[String],
    val animals:       List[String],
    val cigarettes:    List[String])
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?