Avatar of cjvolkert

cjvolkert's solution

to Zebra Puzzle in the Scala Track

Published at Sep 01 2019 · 0 comments
Test suite

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.


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.


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 {

  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 colors =  List("red", "green", "yellow", "ivory", "blue").combinations(5).flatMap(_.permutations).toList
  val nations =  List(Englishman, Ukrainian, Japanese, Norwegian, Spaniard).combinations(5).flatMap(_.permutations).toList
  val cigaretts =  List("Old Gold", "Kools", "Chesterfields", "Lucky Strike", "Parliaments").combinations(5).flatMap(_.permutations).toList
  val beverages =  List("coffee", "tea", "milk", "orange juice", "water").combinations(5).flatMap(_.permutations).toList
  val animals =  List("zebra", "snails", "dog", "fox", "horse").combinations(5).flatMap(_.permutations).toList

  val solutions =  
  for {
      bev <- beverages 
        if bev(2) == "milk" // 9
      col <- colors  
        if (   col.indexOf("green")  == bev.indexOf("coffee") //4
            && col.indexOf("green") +1  == col.indexOf("ivory") ) //6
      nat <- nations
        if (   nat.indexOf(Norwegian) == 0  //10
            && col.indexOf("red") == nat.indexOf(Englishman) //2
            && (Math.abs(col.indexOf("blue") - nat.indexOf(Norwegian)) == 1) //15
            && bev.indexOf("tea") ==  nat.indexOf(Ukrainian)      //5
      cig <- cigaretts
        if(    bev.indexOf("orange juice") == cig.indexOf("Lucky Strike")  //13
            && nat.indexOf(Japanese) == cig.indexOf("Parliaments") //14
      ani <- animals
        if(    ani.indexOf("snails") == cig.indexOf("Old Gold") //7
            && ani.indexOf("dog") == nat.indexOf(Spaniard) //3
            && Math.abs(ani.indexOf("fox") - cig.indexOf("Chesterfields")) == 1 //11
            && cig.indexOf("Kools") == col.indexOf("yellow") //8
            && Math.abs(cig.indexOf("Kools") - ani.indexOf("horse")) == 1 //12
    yield(bev, col, nat, cig, ani) 
  lazy val solve: Solution =  {
    val solution = solutions(0)
    val zebraOwner = solution._3(solution._5.indexOf("zebra"))
    val waterTrinker =  solution._3(solution._1.indexOf("water"))

    Solution(waterTrinker, zebraOwner)

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?