|
| 1 | +defmodule ZebraPuzzle do |
| 2 | + @nationalities ~w(englishman norwegian ukrainian japanese spaniard)a |
| 3 | + @colors ~w(red green ivory yellow blue)a |
| 4 | + @drinks ~w(coffee tea milk orange_juice water)a |
| 5 | + @pets ~w(dog snails fox horse zebra)a |
| 6 | + @cigarettes ~w{old_gold kool chesterfield lucky_strike parliament}a |
| 7 | + |
| 8 | + @doc """ |
| 9 | + Determine who drinks the water |
| 10 | + """ |
| 11 | + @spec drinks_water() :: atom |
| 12 | + def drinks_water() do |
| 13 | + [%{nationality: nationality}] = |
| 14 | + solve_puzzle() |
| 15 | + |> Enum.filter(fn %{drink: drink} -> drink == :water end) |
| 16 | + |
| 17 | + nationality |
| 18 | + end |
| 19 | + |
| 20 | + @doc """ |
| 21 | + Determine who owns the zebra |
| 22 | + """ |
| 23 | + @spec owns_zebra() :: atom |
| 24 | + def owns_zebra() do |
| 25 | + [%{nationality: nationality}] = |
| 26 | + solve_puzzle() |
| 27 | + |> Enum.filter(fn %{pet: pet} -> pet == :zebra end) |
| 28 | + |
| 29 | + nationality |
| 30 | + end |
| 31 | + |
| 32 | + def solve_puzzle() do |
| 33 | + # |
| 34 | + # Step 0: Consider all possible combinations of values |
| 35 | + # |
| 36 | + possibilities = |
| 37 | + Enum.flat_map(1..5, fn order -> |
| 38 | + Enum.flat_map(@colors, fn color -> |
| 39 | + Enum.flat_map(@drinks, fn drink -> |
| 40 | + Enum.flat_map(@nationalities, fn nationality -> |
| 41 | + Enum.flat_map(@cigarettes, fn cigarette -> |
| 42 | + Enum.map(@pets, fn pet -> |
| 43 | + %{ |
| 44 | + order: order, |
| 45 | + color: color, |
| 46 | + drink: drink, |
| 47 | + nationality: nationality, |
| 48 | + cigarette: cigarette, |
| 49 | + pet: pet |
| 50 | + } |
| 51 | + end) |
| 52 | + end) |
| 53 | + end) |
| 54 | + end) |
| 55 | + end) |
| 56 | + end) |
| 57 | + |
| 58 | + # |
| 59 | + # Step 1: Add the direct constraints and filter possibilities |
| 60 | + # |
| 61 | + possibilities |
| 62 | + # The Englishman lives in the red house. |
| 63 | + |> filter_direct(:color, :red, :nationality, :englishman) |
| 64 | + # The Spaniard owns the dog. |
| 65 | + |> filter_direct(:nationality, :spaniard, :pet, :dog) |
| 66 | + # Coffee is drunk in the green house. |
| 67 | + |> filter_direct(:drink, :coffee, :color, :green) |
| 68 | + # The Ukrainian drinks tea. |
| 69 | + |> filter_direct(:drink, :tea, :nationality, :ukrainian) |
| 70 | + # The Old Gold smoker owns snails. |
| 71 | + |> filter_direct(:cigarette, :old_gold, :pet, :snails) |
| 72 | + # Kools are smoked in the yellow house. |
| 73 | + |> filter_direct(:cigarette, :kool, :color, :yellow) |
| 74 | + # Milk is drunk in the middle house. |
| 75 | + |> filter_direct(:drink, :milk, :order, 3) |
| 76 | + # The Norwegian lives in the first house. |
| 77 | + |> filter_direct(:nationality, :norwegian, :order, 1) |
| 78 | + # The Lucky Strike smoker drinks orange juice. |
| 79 | + |> filter_direct(:cigarette, :lucky_strike, :drink, :orange_juice) |
| 80 | + # The Japanese smokes Parliaments. |
| 81 | + |> filter_direct(:cigarette, :parliament, :nationality, :japanese) |
| 82 | + # |
| 83 | + # Step 2: Add indirect constraints (relations with neighbors) |
| 84 | + # |
| 85 | + |> filter_by_neighbors |
| 86 | + # |
| 87 | + # Step 3: Check if some values happen to be possibly in only one house, |
| 88 | + # add those constraints, filter and back to step 2 until all is solved |
| 89 | + # |
| 90 | + |> filter_by_unique_relations |
| 91 | + end |
| 92 | + |
| 93 | + def filter_direct(list, field_1, value_1, field_2, value_2) do |
| 94 | + Enum.filter(list, fn element -> |
| 95 | + cond do |
| 96 | + element[field_1] == value_1 and element[field_2] == value_2 -> true |
| 97 | + element[field_1] == value_1 -> false |
| 98 | + element[field_2] == value_2 -> false |
| 99 | + true -> true |
| 100 | + end |
| 101 | + end) |
| 102 | + end |
| 103 | + |
| 104 | + def filter_by_neighbors(list) do |
| 105 | + next_to = fn n -> [n - 1, n + 1] end |
| 106 | + |
| 107 | + filtered_list = |
| 108 | + list |
| 109 | + # The green house is immediately to the right of the ivory house. |
| 110 | + |> filter_indirect(:color, :green, fn n -> [n - 1] end, :color, :ivory, fn n -> [n + 1] end) |
| 111 | + # The man who smokes Chesterfields lives in the house next to the man with the fox. |
| 112 | + |> filter_indirect(:cigarette, :chesterfield, next_to, :pet, :fox, next_to) |
| 113 | + # Kools are smoked in the house next to the house where the horse is kept. |
| 114 | + |> filter_indirect(:cigarette, :kool, next_to, :pet, :horse, next_to) |
| 115 | + # The Norwegian lives next to the blue house. |
| 116 | + |> filter_indirect(:nationality, :norwegian, next_to, :color, :blue, next_to) |
| 117 | + |
| 118 | + # later filters may influence earlier ones, so we loop until there is no change |
| 119 | + if length(filtered_list) == length(list) do |
| 120 | + list |
| 121 | + else |
| 122 | + filter_by_neighbors(filtered_list) |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + def filter_indirect(list, field_1, value_1, order_1_to_2, field_2, value_2, order_2_to_1) do |
| 127 | + # Get all possible neighbor houses of possibilities with field_1: value_1 |
| 128 | + # Ex: find all possible house numbers that neighbor a green house |
| 129 | + orders_2 = get_orders(list, field_1, value_1, order_1_to_2) |
| 130 | + # Only keep possibilities with field_2: value_2 in that neighborhood |
| 131 | + list2 = filter_neighbors(list, field_2, value_2, orders_2) |
| 132 | + |
| 133 | + # Same from the other perspective |
| 134 | + orders_1 = get_orders(list2, field_2, value_2, order_2_to_1) |
| 135 | + filter_neighbors(list2, field_1, value_1, orders_1) |
| 136 | + end |
| 137 | + |
| 138 | + def get_orders(list, field, value, to_other_order) do |
| 139 | + list |
| 140 | + |> Enum.filter(&(&1[field] == value)) |
| 141 | + |> Enum.map(fn %{order: order} -> to_other_order.(order) end) |
| 142 | + |> Enum.concat() |
| 143 | + |> Enum.uniq() |
| 144 | + |> Enum.filter(fn order -> 1 <= order and order <= 5 end) |
| 145 | + end |
| 146 | + |
| 147 | + def filter_neighbors(list, field, value, orders) do |
| 148 | + Enum.filter(list, fn element -> |
| 149 | + cond do |
| 150 | + element[field] == value and element.order in orders -> true |
| 151 | + element[field] == value -> false |
| 152 | + length(orders) == 1 and element.order == hd(orders) -> false |
| 153 | + true -> true |
| 154 | + end |
| 155 | + end) |
| 156 | + end |
| 157 | + |
| 158 | + def filter_by_unique_relations(list) do |
| 159 | + # Some values happen to exist only in one particular house number |
| 160 | + filter_parameters = |
| 161 | + list |
| 162 | + |> Enum.reduce(%{}, fn house, all -> |
| 163 | + Map.update(all, house[:order], values_to_set(house), fn previous -> |
| 164 | + Map.merge(previous, house, fn _field, val_1, val_2 -> MapSet.put(val_1, val_2) end) |
| 165 | + end) |
| 166 | + end) |
| 167 | + |> Enum.map(fn {order, house} -> |
| 168 | + house |
| 169 | + |> Enum.filter(fn {field, value} -> field != :order and MapSet.size(value) == 1 end) |
| 170 | + |> Enum.map(fn {field, value} -> {order, field, value |> MapSet.to_list() |> hd} end) |
| 171 | + end) |
| 172 | + |> Enum.concat() |
| 173 | + |
| 174 | + # Add those values as constraints and filter |
| 175 | + filtered_list = |
| 176 | + filter_parameters |
| 177 | + |> Enum.reduce(list, fn {order, f, v}, lst -> filter_direct(lst, :order, order, f, v) end) |
| 178 | + # Run the neighbors filter again |
| 179 | + |> filter_by_neighbors |
| 180 | + |
| 181 | + # Loop until no more change (final solution) |
| 182 | + if length(filtered_list) == length(list) do |
| 183 | + filtered_list |
| 184 | + else |
| 185 | + filter_by_unique_relations(filtered_list) |
| 186 | + end |
| 187 | + end |
| 188 | + |
| 189 | + def values_to_set(map) do |
| 190 | + Map.new(map, fn {field, value} -> {field, MapSet.new([value])} end) |
| 191 | + end |
| 192 | +end |
0 commit comments