|
| 1 | +local zebra_puzzle = {} |
| 2 | + |
| 3 | +local function permutations(a) |
| 4 | + local n |
| 5 | + |
| 6 | + return function() |
| 7 | + if not n then |
| 8 | + n = #a |
| 9 | + return a |
| 10 | + end |
| 11 | + |
| 12 | + -- Step 1. Find largest j such that a[j] > a[j + 1]. |
| 13 | + local j = n - 1 |
| 14 | + while j >= 1 and a[j] <= a[j + 1] do |
| 15 | + j = j - 1 |
| 16 | + end |
| 17 | + if j < 1 then return nil end |
| 18 | + |
| 19 | + -- Step 2. Find largest l such that a[j] > a[l], then swap. |
| 20 | + local l = n |
| 21 | + while a[j] <= a[l] do |
| 22 | + l = l - 1 |
| 23 | + end |
| 24 | + a[j], a[l] = a[l], a[j] |
| 25 | + |
| 26 | + -- Step 3. Reverse a[j+1] ... a[n]. |
| 27 | + local lo, hi = j + 1, n |
| 28 | + while lo < hi do |
| 29 | + a[lo], a[hi] = a[hi], a[lo] |
| 30 | + lo = lo + 1 |
| 31 | + hi = hi - 1 |
| 32 | + end |
| 33 | + |
| 34 | + return a |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +local function index_of(t, val) |
| 39 | + for i, v in ipairs(t) do |
| 40 | + if v == val then return i end |
| 41 | + end |
| 42 | + return nil |
| 43 | +end |
| 44 | + |
| 45 | +local function next_to(a, b) |
| 46 | + return math.abs(a - b) == 1 |
| 47 | +end |
| 48 | + |
| 49 | +local water_drinker, zebra_owner |
| 50 | + |
| 51 | +local function solve() |
| 52 | + for colors in permutations( |
| 53 | + { 'yellow', 'red', 'ivory', 'green', 'blue' }) do |
| 54 | + -- 6. The green house is immediately to the right of the ivory house. |
| 55 | + if index_of(colors, 'green') == index_of(colors, 'ivory') + 1 then |
| 56 | + for drinks in permutations( |
| 57 | + { 'water', 'tea', 'orange juice', 'milk', 'coffee' }) do |
| 58 | + -- 4. Coffee is drunk in the green house. |
| 59 | + -- 9. Milk is drunk in the middle house. |
| 60 | + if index_of(drinks, 'coffee') == index_of(colors, 'green') |
| 61 | + and drinks[3] == 'milk' then |
| 62 | + for hobbies in permutations( |
| 63 | + { 'reading', 'painting', 'football', 'dancing', 'chess' }) do |
| 64 | + -- 8. The person in the yellow house is a painter. |
| 65 | + -- 13. The person who plays football drinks orange juice. |
| 66 | + if index_of(hobbies, 'painting') == index_of(colors, 'yellow') |
| 67 | + and index_of(hobbies, 'football') == index_of(drinks, 'orange juice') then |
| 68 | + for nationalities in permutations( |
| 69 | + { 'Ukrainian', 'Spaniard', 'Norwegian', 'Japanese', 'Englishman' }) do |
| 70 | + -- 10. The Norwegian lives in the first house. |
| 71 | + -- 2. The Englishman lives in the red house. |
| 72 | + -- 15. The Norwegian lives next to the blue house. |
| 73 | + -- 5. The Ukrainian drinks tea. |
| 74 | + -- 14. The Japanese person plays chess. |
| 75 | + if nationalities[1] == 'Norwegian' |
| 76 | + and index_of(colors, 'red') == index_of(nationalities, 'Englishman') |
| 77 | + and next_to(index_of(nationalities, 'Norwegian'), index_of(colors, 'blue')) |
| 78 | + and index_of(drinks, 'tea') == index_of(nationalities, 'Ukrainian') |
| 79 | + and index_of(hobbies, 'chess') == index_of(nationalities, 'Japanese') then |
| 80 | + for pets in permutations( |
| 81 | + { 'zebra', 'snail', 'horse', 'fox', 'dog' }) do |
| 82 | + -- 3. The Spaniard owns the dog. |
| 83 | + -- 7. The snail owner likes to go dancing. |
| 84 | + -- 11. The person who enjoys reading lives in the house next to the person with the fox. |
| 85 | + -- 12. The painter's house is next to the house with the horse. |
| 86 | + if index_of(pets, 'dog') == index_of(nationalities, 'Spaniard') |
| 87 | + and index_of(pets, 'snail') == index_of(hobbies, 'dancing') |
| 88 | + and next_to(index_of(hobbies, 'reading'), index_of(pets, 'fox')) |
| 89 | + and next_to(index_of(hobbies, 'painting'), index_of(pets, 'horse')) then |
| 90 | + return nationalities[index_of(drinks, 'water')], |
| 91 | + nationalities[index_of(pets, 'zebra')] |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +local function ensure_solved() |
| 105 | + if not water_drinker then |
| 106 | + water_drinker, zebra_owner = solve() |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 110 | +function zebra_puzzle.drinksWater() |
| 111 | + ensure_solved() |
| 112 | + return water_drinker |
| 113 | +end |
| 114 | + |
| 115 | +function zebra_puzzle.ownsZebra() |
| 116 | + ensure_solved() |
| 117 | + return zebra_owner |
| 118 | +end |
| 119 | + |
| 120 | +return zebra_puzzle |
0 commit comments