|
| 1 | +permute = require 'pl.permute' -- https://luarocks.org/modules/tieske/penlight |
| 2 | + |
| 3 | +toTheRightOf = (a, b) -> a == b + 1 |
| 4 | +nextTo = (a, b) -> toTheRightOf(a, b) or toTheRightOf(b, a) |
| 5 | + |
| 6 | +FIRST = 1 |
| 7 | +MIDDLE = 3 |
| 8 | + |
| 9 | +-- 1. There are five houses. |
| 10 | +-- 2. The Englishman lives in the red house. |
| 11 | +-- 3. The Spaniard owns the dog. |
| 12 | +-- 4. The person in the green house drinks coffee. |
| 13 | +-- 5. The Ukrainian drinks tea. |
| 14 | +-- 6. The green house is immediately to the right of the ivory house. |
| 15 | +-- 7. The snail owner likes to go dancing. |
| 16 | +-- 8. The person in the yellow house is a painter. |
| 17 | +-- 9. The person in the middle house drinks milk. |
| 18 | +-- 10. The Norwegian lives in the first house. |
| 19 | +-- 11. The person who enjoys reading lives in the house next to the person with the fox. |
| 20 | +-- 12. The painter's house is next to the house with the horse. |
| 21 | +-- 13. The person who plays football drinks orange juice. |
| 22 | +-- 14. The Japanese person plays chess. |
| 23 | +-- 15. The Norwegian lives next to the blue house. |
| 24 | + |
| 25 | + |
| 26 | +solve = -> |
| 27 | + -- solve for colours |
| 28 | + for p1 in permute.order_iter {1,2,3,4,5} |
| 29 | + {red, green, ivory, yellow, blue} = p1 |
| 30 | + -- clue 6 |
| 31 | + if toTheRightOf green, ivory |
| 32 | + |
| 33 | + -- solve for nationalities |
| 34 | + for p2 in permute.order_iter {1,2,3,4,5} |
| 35 | + {english, spanish, ukrainian, norwegian, japanese} = p2 |
| 36 | + -- clues 2, 10, 15 |
| 37 | + if english == red and norwegian == FIRST and nextTo norwegian, blue |
| 38 | + nationalities = { |
| 39 | + [english]: 'EnglishMan' |
| 40 | + [spanish]: 'Spaniard' |
| 41 | + [ukrainian]: 'Ukrainian' |
| 42 | + [norwegian]: 'Norwegian' |
| 43 | + [japanese]: 'Japanese' |
| 44 | + } |
| 45 | + |
| 46 | + -- solve for beverages |
| 47 | + for p3 in permute.order_iter {1,2,3,4,5} |
| 48 | + {coffee, tea, milk, orangeJuice, water} = p3 |
| 49 | + -- clues 4, 5, 9 |
| 50 | + if coffee == green and ukrainian == tea and milk == MIDDLE |
| 51 | + |
| 52 | + -- solve for hobbies |
| 53 | + for p4 in permute.order_iter {1,2,3,4,5} |
| 54 | + {dancing, painting, reading, football, chess} = p4 |
| 55 | + -- clues 8, 13, 14 |
| 56 | + if painting == yellow and football == orangeJuice and japanese == chess |
| 57 | + |
| 58 | + -- solve for pets |
| 59 | + for p5 in permute.order_iter {1,2,3,4,5} |
| 60 | + {dog, snails, fox, horse, zebra} = p5 |
| 61 | + -- clues 3, 7, 11, 12 |
| 62 | + if spanish == dog and dancing == snails and nextTo(reading, fox) and nextTo painting, horse |
| 63 | + return { |
| 64 | + waterDrinker: nationalities[water] |
| 65 | + zebraOwner: nationalities[zebra] |
| 66 | + } |
| 67 | + return waterDrinker: '?', zebraOwner: '?' |
| 68 | + |
| 69 | +SOLUTION = solve! |
| 70 | + |
| 71 | +{ |
| 72 | + drinksWater: -> SOLUTION.waterDrinker |
| 73 | + ownsZebra: -> SOLUTION.zebraOwner |
| 74 | +} |
0 commit comments