Avatar of petermartin1

petermartin1's solution

to Zebra Puzzle in the Scala Track

Published at Apr 23 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))
import scala.collection.JavaConverters._

object ZebraPuzzle {

  sealed trait Resident extends Product with Serializable
  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

  val residents = List(Spaniard, Ukrainian, Englishman, Japanese, Norwegian)

  sealed trait Cigarettes extends Product with Serializable
  case object OldGold extends Cigarettes
  case object Kools extends Cigarettes
  case object Chesterfield extends Cigarettes
  case object LuckyStrike extends Cigarettes
  case object Parliaments extends Cigarettes

  val cigs = List(OldGold, Kools, Chesterfield, LuckyStrike, Parliaments)

  sealed trait Drinks extends Product with Serializable
  case object Coffee extends Drinks
  case object Tea extends Drinks
  case object Milk extends Drinks
  case object  OrangeJuice extends Drinks
  case object Water extends Drinks

  val drinks = List(Coffee, Tea, Milk, OrangeJuice, Water)
  val indexOfMiddle: Int = drinks.size/2

  sealed trait Pets extends Product with Serializable
  case object Dog extends Pets
  case object Fox extends Pets
  case object Horse extends Pets
  case object Snails extends Pets
  case object Zebra extends Pets

  val pets = List(Dog, Fox, Horse, Snails, Zebra)

  sealed trait Colors extends Product with Serializable
  case object Red extends Colors
  case object Green extends Colors
  case object Ivory extends Colors
  case object Yellow extends Colors
  case object Blue extends Colors

  val colors = List(Red, Green, Ivory, Yellow, Blue)

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

  def aNextToB[A,B](a: A, aList: List[A], b: B, bList: List[B]): Boolean =  Math.abs(differenceOfIndex(a, aList, b, bList)) == 1

  def aHasB[A,B](a: A, aList: List[A], b: B, bList: List[B]): Boolean =  aList(bList.indexOf(b)) == a

  def aToRightOfB[A, B](a: A, aList: List[A], b: B, bList: List[B]): Boolean = differenceOfIndex(a, aList, b, bList) == 1

  private def differenceOfIndex[B, A](a: A, aList: List[A], b: B, bList: List[B]) = {
    aList.indexOf(a) - bList.indexOf(b)

  private def initialColorCheck(colorList: List[Colors], residentList: List[Resident]): Boolean = {
    aHasB(Englishman, residentList, Red, colorList) &&
      aNextToB(Norwegian, residentList, Blue, colorList) &&
      aToRightOfB(Green, colorList, Ivory, colorList)

  private def initialDrinkCheck(residentList: _root_.scala.collection.immutable.List[ZebraPuzzle.Resident], colorList: _root_.scala.collection.immutable.List[ZebraPuzzle.Colors], drinkList: _root_.scala.collection.immutable.List[ZebraPuzzle.Drinks]) = {
    drinkList(indexOfMiddle) == Milk &&
      aHasB(Coffee, drinkList, Green, colorList) &&
      aHasB(Tea, drinkList, Ukrainian, residentList)


  private def whoDoesWhat[A](a: A, aList: List[A], residentList: List[Resident]): Resident = residentList(aList.indexOf(a))

  lazy val solve: Solution = (for {
    residentList <- residents.permutations if residentList(0) == Norwegian
    colorList <- colors.permutations if initialColorCheck(colorList, residentList)
    petList <- pets.permutations if aHasB(Spaniard, residentList, Dog, petList)
    drinkList <- drinks.permutations if initialDrinkCheck(residentList, colorList, drinkList)
    cigaretteList <- cigs.permutations
    if aHasB(OldGold, cigaretteList, Snails, petList) &&
      aNextToB(Chesterfield, cigaretteList, Fox, petList) &&
      aHasB(Kools, cigaretteList, Yellow, colorList) &&
      aNextToB(Kools, cigaretteList, Horse, petList) &&
      aHasB(LuckyStrike, cigaretteList, OrangeJuice, drinkList) &&
      aHasB(Japanese, residentList, Parliaments, cigaretteList)
    whoDrinksWater = whoDoesWhat(Water, drinkList, residentList)
    whoOwnsZebra = whoDoesWhat(Zebra, petList, residentList)
  } yield Solution(whoDrinksWater, whoOwnsZebra)).toList(0)


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?