Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exercise zebra-puzzle #959

Merged
merged 5 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,14 @@
]
},
{
"slug": "zebra-puzzle",
"name": "Zebra Puzzle",
"uuid": "2cae2796-5c05-4159-b3e7-9f5653f0bc9b",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
}
ryanplusplus marked this conversation as resolved.
Show resolved Hide resolved
"slug": "knapsack",
"name": "Knapsack",
"uuid": "caf3784a-400f-425d-b537-a903175503ff",
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/zebra-puzzle/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

Solve the zebra puzzle.

1. There are five houses.
2. The Englishman lives in the red house.
3. The Spaniard owns the dog.
4. Coffee is drunk in the green house.
5. The Ukrainian drinks tea.
6. The green house is immediately to the right of the ivory house.
7. The Old Gold smoker owns snails.
8. Kools are smoked in the yellow house.
9. Milk is drunk in the middle house.
10. The Norwegian lives in the first house.
11. The man who smokes Chesterfields lives in the house next to the man with the fox.
12. Kools are smoked in the house next to the house where the horse is kept.
13. The Lucky Strike smoker drinks orange juice.
14. The Japanese smokes Parliaments.
15. The Norwegian lives next to the blue house.

Each of the five houses is painted a different color, and their inhabitants are of different national extractions, own different pets, drink different beverages and smoke different brands of cigarettes.

Which of the residents drinks water?
Who owns the zebra?
21 changes: 21 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"siebenschlaefer"
],
"files": {
"solution": [
"zebra_puzzle.c",
"zebra_puzzle.h"
],
"test": [
"test_zebra_puzzle.c"
],
"example": [
".meta/example.c",
".meta/example.h"
]
},
"blurb": "Solve the zebra puzzle.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Zebra_Puzzle"
}
182 changes: 182 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#include "zebra_puzzle.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>

enum {
NUM_HOUSES = 5
};

static inline void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}

/// Transform `order` into its next lexicographically larger permutation.
///
/// return true if on success
/// return false if parameter is already the largest possible permutation
static bool next_permutation(int order[NUM_HOUSES], int by_value[NUM_HOUSES])
{
const size_t len = NUM_HOUSES;

// find the rightmost element that is smaller than its right neighbor
size_t idx1 = len - 2;
while (idx1 < len && order[idx1] >= order[idx1 + 1])
--idx1;

// if no such element has been found we've reached the final permutation
if (idx1 >= len)
return false;

// find the rightmost element that is larger than order[idx1]
size_t idx2 = len - 1;
while (order[idx1] >= order[idx2])
--idx2;

// permute the elements
swap(order + idx1, order + idx2);
for (size_t a = idx1 + 1, b = len - 1; a < b; ++a, --b)
swap(order + a, order + b);

// update the affected elements of by_value
for (size_t i = idx1; i < NUM_HOUSES; ++i)
by_value[order[i]] = i;
return true;
}

enum nationality { ENGLISH, JAPANESE, NORWEGIAN, SPANISH, UKRAINIAN };
enum color { BLUE, GREEN, IVORY, RED, YELLOW };
enum animal { DOG, FOX, HORSE, SNAILS, ZEBRA };
enum drink { COFFEE, MILK, ORANGE_JUICE, TEA, WATER };
enum smoke { CHESTERFIELD, KOOL, LUCKY_STRIKE, OLD_GOLD, PARLIAMENT };

static void solve(const char **out_drinks_water, const char **out_owns_zebra)
{
int num_solutions = 0;
int drinks_water = -1;
int owns_zebra = -1;

int colors[] = { BLUE, GREEN, IVORY, RED, YELLOW };
int house_by_color[] = { 0, 1, 2, 3, 4 };
do {
// 6. The green house is immediately to the right of the ivory house.
if (house_by_color[GREEN] - 1 != house_by_color[IVORY])
continue;

int nationalities[] = { ENGLISH, JAPANESE, NORWEGIAN, SPANISH, UKRAINIAN };
int house_by_nationality[] = { 0, 1, 2, 3, 4 };
do {
// 2. The Englishman lives in the red house.
if (house_by_nationality[ENGLISH] != house_by_color[RED])
continue;

// 10. The Norwegian lives in the first house.
if (house_by_nationality[NORWEGIAN] != 0)
continue;

// 15. The Norwegian lives next to the blue house.
if (abs(house_by_nationality[NORWEGIAN] - house_by_color[BLUE]) != 1)
continue;

int pets[] = { DOG, FOX, HORSE, SNAILS, ZEBRA };
int house_by_pet[] = { 0, 1, 2, 3, 4 };
do {
// 3. The Spaniard owns the dog.
if (house_by_nationality[SPANISH] != house_by_pet[DOG])
continue;

int beverages[] = { COFFEE, MILK, ORANGE_JUICE, TEA, WATER };
int house_by_beverage[] = { 0, 1, 2, 3, 4 };
do {
// 4. Coffee is drunk in the green house.
if (house_by_beverage[COFFEE] != house_by_color[GREEN])
continue;

// 5. The Ukrainian drinks tea.
if (house_by_nationality[UKRAINIAN] != house_by_beverage[TEA])
continue;

// 9. Milk is drunk in the middle house.
if (house_by_beverage[MILK] != NUM_HOUSES / 2)
continue;

int brands[] = { CHESTERFIELD, KOOL, LUCKY_STRIKE,
OLD_GOLD, PARLIAMENT };
int house_by_brand[] = { 0, 1, 2, 3, 4 };
do {
// 7. The Old Gold smoker owns snails.
if (house_by_brand[OLD_GOLD] != house_by_pet[SNAILS])
continue;

// 8. Kools are smoked in the yellow house.
if (house_by_brand[KOOL] != house_by_color[YELLOW])
continue;

// 11. The man who smokes Chesterfields lives in the
// house next to the man with the fox.
if (abs(house_by_brand[CHESTERFIELD] - house_by_pet[FOX]) != 1)
continue;

// 12. Kools are smoked in the house next to the house
// where the horse is kept.
if (abs(house_by_brand[KOOL] - house_by_pet[HORSE]) != 1)
continue;

// 13. The Lucky Strike smoker drinks orange juice.
if (house_by_brand[LUCKY_STRIKE] != house_by_beverage[ORANGE_JUICE])
continue;

// 14. The Japanese smokes Parliaments.
if (house_by_nationality[JAPANESE] != house_by_brand[PARLIAMENT])
continue;

assert(num_solutions == 0);
++num_solutions;
drinks_water = nationalities[house_by_beverage[WATER]];
owns_zebra = nationalities[house_by_pet[ZEBRA]];

} while (next_permutation(brands, house_by_brand));
} while (next_permutation(beverages, house_by_beverage));
} while (next_permutation(pets, house_by_pet));
} while (next_permutation(nationalities, house_by_nationality));
} while (next_permutation(colors, house_by_color));

assert(num_solutions == 1);
switch (drinks_water)
{
case ENGLISH: *out_drinks_water = "English"; break;
case JAPANESE: *out_drinks_water = "Japanese"; break;
case NORWEGIAN: *out_drinks_water = "Norwegian"; break;
case SPANISH: *out_drinks_water = "Spanish"; break;
case UKRAINIAN: *out_drinks_water = "Ukrainian"; break;
default: assert(false);
}
switch (owns_zebra)
{
case ENGLISH: *out_owns_zebra = "English"; break;
case JAPANESE: *out_owns_zebra = "Japanese"; break;
case NORWEGIAN: *out_owns_zebra = "Norwegian"; break;
case SPANISH: *out_owns_zebra = "Spanish"; break;
case UKRAINIAN: *out_owns_zebra = "Ukrainian"; break;
default: assert(false);
}
}

const char *drinks_water(void)
{
const char *result, *dummy;
solve(&result, &dummy);
return result;
}

const char *owns_zebra(void)
{
const char *result, *dummy;
solve(&dummy, &result);
return result;
}

10 changes: 10 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef ZEBRA_PUZZLE_H
#define ZEBRA_PUZZLE_H

/// Determine the nationality of the resident who drinks water.
const char *drinks_water(void);

/// Determine the nationality of the resident who owns the zebra.
const char *owns_zebra(void);

#endif // ZEBRA_PUZZLE_H
16 changes: 16 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[16efb4e4-8ad7-4d5e-ba96-e5537b66fd42]
description = "resident who drinks water"

[084d5b8b-24e2-40e6-b008-c800da8cd257]
description = "resident who owns zebra"
37 changes: 37 additions & 0 deletions exercises/practice/zebra-puzzle/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### If you wish to use extra libraries (math.h for instance),
### add their flags here (-lm in our case) in the "LIBS" variable.

LIBS = -lm

###
CFLAGS = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror
CFLAGS += -Wmissing-declarations
CFLAGS += -DUNITY_SUPPORT_64 -DUNITY_OUTPUT_COLOR

ASANFLAGS = -fsanitize=address
ASANFLAGS += -fno-common
ASANFLAGS += -fno-omit-frame-pointer

.PHONY: test
test: tests.out
@./tests.out

.PHONY: memcheck
memcheck: ./*.c ./*.h
@echo Compiling $@
@$(CC) $(ASANFLAGS) $(CFLAGS) test-framework/unity.c ./*.c -o memcheck.out $(LIBS)
@./memcheck.out
@echo "Memory check passed"

.PHONY: clean
clean:
rm -rf *.o *.out *.out.dSYM

tests.out: ./*.c ./*.h
@echo Compiling $@
@$(CC) $(CFLAGS) test-framework/unity.c ./*.c -o tests.out $(LIBS)
Loading