Skip to content

Commit 534ab21

Browse files
lua-format
1 parent f198304 commit 534ab21

1 file changed

Lines changed: 22 additions & 27 deletions

File tree

exercises/practice/zebra-puzzle/.meta/example.lua

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ local function permutations(a)
1414
while j >= 1 and a[j] <= a[j + 1] do
1515
j = j - 1
1616
end
17-
if j < 1 then return nil end
17+
if j < 1 then
18+
return nil
19+
end
1820

1921
-- Step 2. Find largest l such that a[j] > a[l], then swap.
2022
local l = n
@@ -37,7 +39,9 @@ end
3739

3840
local function index_of(t, val)
3941
for i, v in ipairs(t) do
40-
if v == val then return i end
42+
if v == val then
43+
return i
44+
end
4145
end
4246
return nil
4347
end
@@ -49,46 +53,37 @@ end
4953
local water_drinker, zebra_owner
5054

5155
local function solve()
52-
for colors in permutations(
53-
{ 'yellow', 'red', 'ivory', 'green', 'blue' }) do
56+
for colors in permutations({ 'yellow', 'red', 'ivory', 'green', 'blue' }) do
5457
-- 6. The green house is immediately to the right of the ivory house.
5558
if index_of(colors, 'green') == index_of(colors, 'ivory') + 1 then
56-
for drinks in permutations(
57-
{ 'water', 'tea', 'orange juice', 'milk', 'coffee' }) do
59+
for drinks in permutations({ 'water', 'tea', 'orange juice', 'milk', 'coffee' }) do
5860
-- 4. Coffee is drunk in the green house.
5961
-- 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
62+
if index_of(drinks, 'coffee') == index_of(colors, 'green') and drinks[3] == 'milk' then
63+
for hobbies in permutations({ 'reading', 'painting', 'football', 'dancing', 'chess' }) do
6464
-- 8. The person in the yellow house is a painter.
6565
-- 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
66+
if index_of(hobbies, 'painting') == index_of(colors, 'yellow') and index_of(hobbies, 'football') ==
67+
index_of(drinks, 'orange juice') then
68+
for nationalities in permutations({ 'Ukrainian', 'Spaniard', 'Norwegian', 'Japanese', 'Englishman' }) do
7069
-- 10. The Norwegian lives in the first house.
7170
-- 2. The Englishman lives in the red house.
7271
-- 15. The Norwegian lives next to the blue house.
7372
-- 5. The Ukrainian drinks tea.
7473
-- 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
74+
if nationalities[1] == 'Norwegian' and index_of(colors, 'red') == index_of(nationalities, 'Englishman') and
75+
next_to(index_of(nationalities, 'Norwegian'), index_of(colors, 'blue')) and index_of(drinks, 'tea') ==
76+
index_of(nationalities, 'Ukrainian') and index_of(hobbies, 'chess') ==
77+
index_of(nationalities, 'Japanese') then
78+
for pets in permutations({ 'zebra', 'snail', 'horse', 'fox', 'dog' }) do
8279
-- 3. The Spaniard owns the dog.
8380
-- 7. The snail owner likes to go dancing.
8481
-- 11. The person who enjoys reading lives in the house next to the person with the fox.
8582
-- 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')]
83+
if index_of(pets, 'dog') == index_of(nationalities, 'Spaniard') and index_of(pets, 'snail') ==
84+
index_of(hobbies, 'dancing') and next_to(index_of(hobbies, 'reading'), index_of(pets, 'fox')) and
85+
next_to(index_of(hobbies, 'painting'), index_of(pets, 'horse')) then
86+
return nationalities[index_of(drinks, 'water')], nationalities[index_of(pets, 'zebra')]
9287
end
9388
end
9489
end

0 commit comments

Comments
 (0)