Avatar of nickandh

nickandh's solution

to Zebra Puzzle in the Scala Track

Published at Oct 11 2019 · 0 comments
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.

Please see the learning and installation pages if you need any help.

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 extends App {

  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 object Unknown extends Resident

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

  case class Houses(colours: List[String], nationalities: List[String], pets: List[String], smokes: List[String], drinks: List[String])


  def matches(a: (String, List[String]), b: (String, List[String])): Boolean = {
    (a, b) match {
      case ((val1, list1), (val2, list2)) => list1.indexOf( val1 ) == list2.indexOf( val2 )
    }
  }

  def rightOf(left: (String, List[String]), right: (String, List[String])): Boolean = {
    (left, right) match {
      case ((val1, list1), (val2, list2)) => list1.indexOf( val1 ) == (list2.indexOf( val2 ) - 1)
    }
  }

  def nextTo(left: (String, List[String]), right: (String, List[String])): Boolean = {
    (left, right) match {
      case ((val1, list1), (val2, list2)) => (list1.indexOf( val1 ) - list2.indexOf( val2 )).abs == 1
    }
  }

  def positionRules(colours: List[String]): Boolean = {
    //rule 5
    rightOf( ("Ivory", colours), ("Green", colours) )
  }

  def nationalityRules(colours: List[String], nationalities: List[String]): Boolean = {
    //rule 1
    matches( ("English", nationalities), ("Red", colours) ) &&
      //rule 9
      nationalities.indexOf( "Norwegian" ) == 0 &&
      //rule 14
      nextTo( ("Norwegian", nationalities), ("Blue", colours) )
  }

  def drinksRules(colours: List[String], drinks: List[String], nationalities: List[String]): Boolean = {
    //rule 3
    matches( ("coffee", drinks), ("Green", colours) ) &&
      //rule 4
      matches( ("tea", drinks), ("Ukrainian", nationalities) ) &&
      //rule 8
      drinks.indexOf( "milk" ) == 2
  }


  def smokesRules(colours: List[String], smokes: List[String], nationalities: List[String], drinks: List[String]): Boolean = {

    //rule 7
    matches( ("Kools", smokes), ("Yellow", colours) ) &&
      //rule 12
      matches( ("Lucky Strike", smokes), ("orange", drinks) ) &&
      //rule 13
      matches( ("Parliaments", smokes), ("Japanese", nationalities) )
   }

  def petRules(nationalities: List[String], pets: List[String], smokes: List[String]): Boolean = {
    //rule 2
    matches( ("Spanish", nationalities), ("dog", pets) ) &&
      //rule 6
      matches( ("Old Gold", smokes), ("snail", pets) ) &&
      //rule10
      nextTo( ("fox", pets), ("Chesterfields", smokes) ) &&
      //rule 11
      nextTo( ("horse", pets), ("Kools", smokes) )
  }

  def solveIt: Solution = {

    val houseColoursSet = List( "Red", "Green", "Ivory", "Blue", "Yellow" )
    val nationalitiesSet = List( "English", "Spanish", "Ukrainian", "Norwegian", "Japanese" )
    val drinksSet = List( "coffee", "water", "orange", "tea", "milk" )
    val petsSet = List( "zebra", "dog", "fox", "snail", "horse" )
    val smokesSet = List( "Old Gold", "Chesterfields", "Kools", "Lucky Strike", "Parliaments" )
    val natMap = Map( "English" -> Englishman, "Spanish" -> Spaniard, "Ukrainian" -> Ukrainian, "Norwegian" -> Norwegian, "Japanese" -> Japanese )

    val streets =
      for {
        colours <- houseColoursSet.permutations if positionRules (colours)
        nationalities <- nationalitiesSet.permutations if nationalityRules( colours, nationalities  )
        drinks <- drinksSet.permutations if drinksRules( colours, drinks , nationalities )
        smokes <- smokesSet.permutations if smokesRules( colours, smokes, nationalities, drinks )
        pets <- petsSet.permutations if petRules( nationalities,pets, smokes )
      } yield Houses( colours, nationalities, pets, smokes, drinks )

    val street = streets.toList.head
    val waterDrinker = natMap( street.nationalities( street.drinks.indexOf( "water" ) ) )
    val zebraOwner = natMap( street.nationalities( street.pets.indexOf( "zebra" ) ) )
    Solution( waterDrinker, zebraOwner )
  }

  lazy val solve: Solution = solveIt


}

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?