Avatar of burns239

burns239's solution

to Zebra Puzzle in the Scala Track

Published at Jun 03 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 {

  case class House(
  	colour: Colour,
  	resident: Resident, 
  	pet: Pet, 
  	brand: Brand, 
  	drink: Drink

  sealed trait Colour
  case object Red extends Colour
  case object Green extends Colour
  case object Yellow extends Colour
  case object Blue extends Colour
  case object Ivory extends Colour

  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

  sealed trait Pet
  case object Dog extends Pet
  case object Snail extends Pet
  case object Zebra extends Pet
  case object Fox extends Pet
  case object Horse extends Pet

  sealed trait Brand 
  case object OldGold extends Brand
  case object Kools extends Brand
  case object Chesterfield extends Brand
  case object LuckyStrike extends Brand
  case object Parliament extends Brand

  sealed trait Drink
  case object Coffee extends Drink
  case object Tea extends Drink
  case object Milk extends Drink
  case object OrangeJuice extends Drink
  case object Water extends Drink

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

  lazy val solve: Solution = {

  val colours = Seq(Red, Green, Yellow, Blue, Ivory)
  val residents = Seq(Englishman, Spaniard, Ukrainian, Norwegian, Japanese)
	val pets = Seq(Dog, Snail, Zebra, Fox, Horse)
	val brands = Seq(OldGold, Kools, Chesterfield, LuckyStrike, Parliament)
	val drinks = Seq(Coffee, Tea, Milk, OrangeJuice, Water)

	def colourRules: Seq[Colour] => Boolean = {
  		l => 
  		val index = l.indexWhere(_ == Ivory)
  		(index != 4 && l(index + 1) == Green)

  	def residentRules: (Seq[Colour], Seq[Resident]) => Boolean = {
  		(c, r) => r(0) == Norwegian && (c.indexWhere(_ == Red) == r.indexWhere(_ == Englishman))
  				  (r.indexWhere(_ == Norwegian) == c.indexWhere(_ == Blue)+1 ||
  				   r.indexWhere(_ == Norwegian) == c.indexWhere(_ == Blue)-1)

  	def petRules: (Seq[Resident], Seq[Pet]) => Boolean = {
  		(r, p) => (r.indexWhere(_ == Spaniard) == p.indexWhere(_ == Dog))

  	def brandRules: (Seq[Colour], Seq[Resident], Seq[Pet], Seq[Brand]) => Boolean = {
  		(c, r, p, b) => (p.indexWhere(_ == Snail) == b.indexWhere(_ == OldGold)) &&
  						(c.indexWhere(_ == Yellow) == b.indexWhere(_ == Kools)) &&
  						(r.indexWhere(_ == Japanese) == b.indexWhere(_ == Parliament)) &&
  						(p.indexWhere(_ == Fox) == b.indexWhere(_ == Chesterfield)+1 ||
  						 p.indexWhere(_ == Fox) == b.indexWhere(_ == Chesterfield)-1) &&
  						(p.indexWhere(_ == Horse) == b.indexWhere(_ == Kools)+1 ||
  						 p.indexWhere(_ == Horse) == b.indexWhere(_ == Kools)-1)

  	def drinkRules: (Seq[Colour], Seq[Resident], Seq[Brand], Seq[Drink]) => Boolean = {
  		(c, r, b, d) => (d(2) == Milk) &&
  						(d.indexWhere(_ == Coffee) == c.indexWhere(_ == Green)) &&
  						(d.indexWhere(_ == Tea) == r.indexWhere(_ == Ukrainian)) &&
  						(d.indexWhere(_ == OrangeJuice) == b.indexWhere(_ == LuckyStrike))

  	def getWaterDrinker(houses: Seq[House]): Resident = {
  		houses.filter(h => h.drink == Water).head.resident 
  	def getZebraOwner(houses: Seq[House]): Resident = {
  		houses.filter(h => h.pet == Zebra).head.resident

  	lazy val combinations: Seq[Seq[House]] = {
  		for { 
  			c <- colours.permutations.toSeq if colourRules(c)
  			r <- residents.permutations if residentRules(c, r)
  			p <- pets.permutations if petRules(r, p)
  			b <- brands.permutations if brandRules(c, r, p, b)
  			d <- drinks.permutations if drinkRules(c, r, b, d)
  		} yield getHouses(c,r,p,b,d)

  	def getHouses(newColours: Seq[Colour], newResidents: Seq[Resident], newPets: Seq[Pet], newBrands: Seq[Brand], newDrinks: Seq[Drink]): Seq[House] = {
  		for (i <- 0 to 4) yield House(newColours(i), newResidents(i), newPets(i), newBrands(i), newDrinks(i))

  	Solution(getWaterDrinker(combinations.head), getZebraOwner(combinations.head))



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?