I realize this is very repetitive and needs refactored but I am just trying to get a correct result at this point. I figured this was logically sound to start with a “full house” and whittle my way down through the clues repeatedly until it stops whittling and that should be the correct array of houses each with a list of length 1 for each property. I don’t know what the result of buildHouses actually is as I cannot get the DEbug.log function to display anything, no matter where i put it. The result of both my “public” functions is just Nothing and it should be a single nationality. Anyone care to look through this? CHatGPT says it is logically sound with no typos and should work.
type alias House =
{ nationality : List Nationality
, color : List Color
, pet : List Pet
, hobby : List Hobby
, drinks : List Drinks
}
buildHouses : Array House
buildHouses =
let
fullHouse =
{ nationality = allNationalities
, color = allColors
, pet = allPets
, hobby = allHobbies
, drinks = allDrinks
}
--1. There are five houses.
basesHouses =
Array.repeat 5 fullHouse
--10. The Norwegian lives in the first house.
applyClue10 houses =
let
maybeHouse1 =
Array.get 0 houses
maybeHouse1Norwegian =
Maybe.map (\house -> { house | nationality = [Norwegian] }) maybeHouse1
housesWithNorwegianFirst =
case maybeHouse1Norwegian of
Just house ->
Array.set 0 house houses
Nothing ->
houses
removeNorwegianFromOthers index house =
if index == 0 then
house
else
let
notNorwegian =
List.filter (\n -> n /= Norwegian) house.nationality
in
{ house | nationality = notNorwegian }
in
Array.indexedMap removeNorwegianFromOthers housesWithNorwegianFirst
--9. The person in the middle house drinks milk.
applyClue9 houses =
let
maybeHouse3 =
Array.get 2 houses
maybeHouse3Milk =
Maybe.map (\house -> { house | drinks = [Milk] }) maybeHouse3
housesWithMilkMiddle =
case maybeHouse3Milk of
Just house ->
Array.set 2 house houses
Nothing ->
houses
removeMilkFromOthers index house =
if index == 2 then
house
else
let
notMilk =
List.filter (\n -> n /= Milk) house.drinks
in
{ house | drinks = notMilk }
in
Array.indexedMap removeMilkFromOthers housesWithMilkMiddle
--2. The Englishman lives in the red house.
applyClue2 houses =
let
isEnglishmanPossible house =
List.member Englishman house.nationality
isRedPossible house =
List.member Red house.color
filteredColors house =
if not (isEnglishmanPossible house) then
List.filter (\c -> c /= Red) house.color
else
house.color
filteredNationalities house =
if not (isRedPossible house) then
List.filter (\n -> n /= Englishman) house.nationality
else
house.nationality
filterFinal house =
{ house
| color = filteredColors house
, nationality = filteredNationalities house
}
in
Array.map filterFinal houses
--3. The Spaniard owns the dog.
applyClue3 houses =
similar to 2
--4. The person in the green house drinks coffee.
applyClue4 houses =
similar to 2
--5. The Ukrainian drinks tea.
similar to 2
--6. The green house is immediately to the right of the ivory house.
applyClue6 houses =
let
isColorHousePossbileAtIndex color index =
if index >= 0 && index < Array.length houses then
houses
|> Array.get index
|> Maybe.map (\h -> List.member color h.color)
|> Maybe.withDefault False
else
False
isIvoryPossibleAtLeftNeighbor index =
isColorHousePossbileAtIndex Ivory (index - 1)
isGreenPossibleAtRightNeighbor index =
isColorHousePossbileAtIndex Green (index + 1)
applyColorFilters index house =
let
colorsAfterIvoryCheck =
if not (isIvoryPossibleAtLeftNeighbor index) then
List.filter (\c -> c /= Green) house.color
else
house.color
colorsAfterGreenCheck =
if not (isGreenPossibleAtRightNeighbor index) then
List.filter (\c -> c /= Ivory) colorsAfterIvoryCheck
else
colorsAfterIvoryCheck
in
colorsAfterGreenCheck
filterFinal index house =
{ house | color = applyColorFilters index house }
in
Array.indexedMap filterFinal houses
--7. The snail owner likes to go dancing.
applyClue7 houses =
let
isSnailPossible house =
List.member Snail house.pet
isDancingPossible house =
List.member Dancing house.hobby
filteredHobbies house =
if not (isSnailPossible house) then
List.filter (\h -> h /= Dancing) house.hobby
else
house.hobby
filteredPets house =
if not (isDancingPossible house) then
List.filter (\p -> p /= Snail) house.pet
else
house.pet
filterFinal house =
{ house
| hobby = filteredHobbies house
, pet = filteredPets house
}
in
Array.map filterFinal houses
--8.The person in the yellow house is a painter.
applyClue8 houses =
let
isYellowPossible house =
List.member Yellow house.color
isPaintingPossible house =
List.member Painter house.hobby
filteredColors house =
if not (isPaintingPossible house) then
List.filter (\c -> c /= Yellow) house.color
else
house.color
filteredHobbies house =
if not (isYellowPossible house) then
List.filter (\h -> h /= Painter) house.hobby
else
house.hobby
filterFinal house =
{ house
| hobby = filteredHobbies house
, color = filteredColors house
}
in
Array.map filterFinal houses



