Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Zebra Puzzle exercise #1647

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -1567,6 +1567,17 @@
"enumeration"
],
"difficulty": 8
},
{
"slug": "zebra-puzzle",
"name": "Zebra Puzzle",
"uuid": "0c585fee-0afc-44c3-b5bf-7f29ed6f5b42",
"practices": [],
"prerequisites": [
"arrays",
"advanced-enumeration"
],
"difficulty": 7
}
]
},
24 changes: 24 additions & 0 deletions exercises/practice/zebra-puzzle/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

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?
17 changes: 17 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["fpsvogel"],
"files": {
"solution": [
"zebra_puzzle.rb"
],
"test": [
"zebra_puzzle_test.rb"
],
"example": [
".meta/example.rb"
]
},
"blurb": "Solve the zebra puzzle.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Zebra_Puzzle"
}
150 changes: 150 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
CATEGORIES = {
color: %i[
red
green
blue
yellow
ivory
].freeze,
nationality: %i[
englishman
spaniard
ukrainian
norwegian
japanese
].freeze,
beverage: %i[
coffee
tea
milk
orange_juice
water
].freeze,
cigarette: %i[
old_gold
kool
chesterfield
lucky_strike
parliament
].freeze,
pet: %i[
dog
snails
fox
horse
zebra
].freeze
}.freeze

House = Data.define(*CATEGORIES.keys)

module ZebraPuzzle
def self.water_drinker
houses.find { _1.beverage == :water }.nationality.to_s.capitalize
end

def self.zebra_owner
houses.find { _1.pet == :zebra }.nationality.to_s.capitalize
end

private_class_method def self.houses
@houses ||= ZebraPuzzleSolver.new.final_row_of_houses
end
end

class ZebraPuzzleSolver
def initialize
@table = null_table
end

def final_row_of_houses
return @table unless @table == null_table

CATEGORIES.each_key do |key|
add_into_table!(key)
send("filter_by_#{key}!")
end

raise "More than one final possibility exists" if @table.count > 1

@table.first
end

private
def null_table
null_house = CATEGORIES.keys.to_h { |key| [key, nil] }

# Clue: There are five houses.
[[null_house] * 5]
end

def add_into_table!(new_key)
permutations = CATEGORIES.fetch(new_key).permutation

@table.map! do |houses|
permutations.map do |new_values|
houses.zip(new_values).map do |house, new_value|
House.new(**house.to_h.merge(new_key => new_value))
end
end
end.flatten!(1)
end

def filter_by_color!
@table.select! do |houses|
# Clue: The green house is immediately to the right of the ivory house.
houses.index { _1.color == :green } == houses.index { _1.color == :ivory } + 1 &&
# (Clue: The second house is blue. From these clues:)
# - The Norwegian lives in the first house.
# - The Norwegian lives next to the blue house.
houses.index { _1.color == :blue } == 1
end
end

def filter_by_nationality!
@table.select! do |houses|
# Clue: The Englishman lives in the red house.
houses.any? { _1.nationality == :englishman && _1.color == :red } &&
# Clue: The Norwegian lives in the first house.
houses.first.nationality == :norwegian
end
end

def filter_by_beverage!
@table.select! do |houses|
# Clue: Coffee is drunk in the green house.
houses.any? { _1.beverage == :coffee && _1.color == :green } &&
# Clue: The Ukrainian drinks tea.
houses.any? { _1.nationality == :ukrainian && _1.beverage == :tea } &&
# Clue: Milk is drunk in the middle house.
houses[2].beverage == :milk
end
end

def filter_by_cigarette!
@table.select! do |houses|
# Clue: Kools are smoked in the yellow house.
houses.any? { _1.cigarette == :kool && _1.color == :yellow } &&
# Clue: The Lucky Strike smoker drinks orange juice.
houses.any? { _1.cigarette == :lucky_strike && _1.beverage == :orange_juice } &&
# Clue: The Japanese smokes Parliaments.
houses.any? { _1.nationality == :japanese && _1.cigarette == :parliament }
end
end

# rubocop:disable Metrics/AbcSize

def filter_by_pet!
@table.select! do |houses|
# Clue: The Spaniard owns the dog.
houses.any? { _1.nationality == :spaniard && _1.pet == :dog } &&
# Clue: The Old Gold smoker owns snails.
houses.any? { _1.cigarette == :old_gold && _1.pet == :snails } &&
# Clue: The man who smokes Chesterfields lives in the house next to the man with the fox.
(houses.index { _1.cigarette == :chesterfield } - houses.index { _1.pet == :fox }).abs == 1 &&
# Clue: Kools are smoked in the house next to the house where the horse is kept.
(houses.index { _1.cigarette == :kool } - houses.index { _1.pet == :horse }).abs == 1
end
end
# rubocop:enable Metrics/AbcSize
end
16 changes: 16 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[16efb4e4-8ad7-4d5e-ba96-e5537b66fd42]
description = "resident who drinks water"

[084d5b8b-24e2-40e6-b008-c800da8cd257]
description = "resident who owns zebra"
6 changes: 6 additions & 0 deletions exercises/practice/zebra-puzzle/zebra_puzzle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=begin
Write your code for the 'Zebra Puzzle' exercise in this file. Make the tests in
`zebra_puzzle_test.rb` pass.
To get started with TDD, see the `README.md` file in your
`ruby/zebra-puzzle` directory.
=end
12 changes: 12 additions & 0 deletions exercises/practice/zebra-puzzle/zebra_puzzle_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'minitest/autorun'
require_relative 'zebra_puzzle'

class ZebraPuzzleTest < Minitest::Test
def test_resident_who_drinks_water
assert_equal ZebraPuzzle.water_drinker, "Norwegian"
end

def test_resident_who_owns_zebra
assert_equal ZebraPuzzle.zebra_owner, "Japanese"
end
end