Skip to content

Commit

Permalink
Improve the zebra-puzzle.
Browse files Browse the repository at this point in the history
  • Loading branch information
siebenschlaefer committed Feb 28, 2024
1 parent 1f13e2c commit 4a76501
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 62 deletions.
81 changes: 25 additions & 56 deletions exercises/practice/zebra-puzzle/.meta/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static bool next_permutation(int order[NUM_HOUSES], int by_value[NUM_HOUSES])
swap(order + a, order + b);

// update the affected elements of by_value
for (size_t i = idx1; i < NUM_HOUSES; ++i)
for (size_t i = idx1; i < len; ++i)
by_value[order[i]] = i;
return true;
}
Expand All @@ -52,11 +52,28 @@ 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)
static const char *nationality_to_string(enum nationality nationality)
{
switch (nationality) {
case ENGLISH:
return "English";
case JAPANESE:
return "Japanese";
case NORWEGIAN:
return "Norwegian";
case SPANISH:
return "Spanish";
case UKRAINIAN:
return "Ukrainian";
}
assert(false);
}

struct solution solve_puzzle()
{
int num_solutions = 0;
int drinks_water = -1;
int owns_zebra = -1;
enum nationality drinks_water = -1;
enum nationality owns_zebra = -1;

int colors[] = { BLUE, GREEN, IVORY, RED, YELLOW };
int house_by_color[] = { 0, 1, 2, 3, 4 };
Expand Down Expand Up @@ -148,56 +165,8 @@ static void solve(const char **out_drinks_water, const char **out_owns_zebra)
} 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;
return (struct solution) {
.drinks_water = nationality_to_string(drinks_water),
.owns_zebra = nationality_to_string(owns_zebra),
};
}
6 changes: 4 additions & 2 deletions exercises/practice/zebra-puzzle/test_zebra_puzzle.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ void tearDown(void)

static void test_who_drinks_water(void)
{
TEST_ASSERT_EQUAL_STRING("Norwegian", drinks_water());
struct solution solution = solve_puzzle();
TEST_ASSERT_EQUAL_STRING("Norwegian", solution.drinks_water);
}

static void test_who_owns_the_zebra(void)
{
TEST_IGNORE(); // delete this line to run test
TEST_ASSERT_EQUAL_STRING("Japanese", owns_zebra());
struct solution solution = solve_puzzle();
TEST_ASSERT_EQUAL_STRING("Japanese", solution.owns_zebra);
}

int main(void)
Expand Down
10 changes: 6 additions & 4 deletions exercises/practice/zebra-puzzle/zebra_puzzle.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef ZEBRA_PUZZLE_H
#define ZEBRA_PUZZLE_H

/// Determine the nationality of the resident who drinks water.
const char *drinks_water(void);
struct solution
{
const char *drinks_water;
const char *owns_zebra;
};

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

#endif

0 comments on commit 4a76501

Please sign in to comment.