#include <stdio.h>

#define PERSON_BRITISH   0
#define PERSON_SWEDISH   1
#define PERSON_DANISH    2
#define PERSON_NORWEGIAN 3
#define PERSON_GERMAN    4

char *nationalities[5] = {"British", "Swedish", "Danish", "Norwegian", "German"};

#define PET_DOG   0
#define PET_CAT   1
#define PET_BIRD  2
#define PET_HORSE 3
#define PET_FISH  4

char *pets[5] = {"Dog", "Cat", "Bird", "Horse", "Fish"};

#define DRINK_TEA    0
#define DRINK_COFFEE 1
#define DRINK_MILK   2
#define DRINK_BEER   3
#define DRINK_WATER  4

char *drinks[5] = {"Tea", "Coffee", "Milk", "Beer", "Water"};

#define SMOKE_PALLMALL   0
#define SMOKE_DUNHILL    1
#define SMOKE_BLEND      2
#define SMOKE_BLUEMASTER 3
#define SMOKE_PRINCE     4

char *smokes[5] = {"Pall Mall", "Dunhill", "Blend", "Blue Master", "Prince"};

#define HOUSE_RED    0
#define HOUSE_WHITE  1
#define HOUSE_GREEN  2
#define HOUSE_YELLOW 3
#define HOUSE_BLUE   4

char *houses[5] = {"Red", "White", "Green", "Yellow", "Blue"};

int person[5], pet[5], drink[5], smoke[5], house[5];

int british, swedish, danish, norwegian, german;
int dog, cat, bird, horse, fish;
int tea, coffee, milk, beer, water;
int pallmall, dunhill, blend, bluemaster, prince;
int red, white, green, yellow, blue;

// Choses 1-PERSON, 2-then PET, then 3-DRINK, then 4-SMOKE, then 5-HOUSE

/*
1 - The Norwegian lives in the leftmost house.

2 - The Swede keeps a dog.

3 - The Dane drinks tea.
3 - The person in the center house drinks milk.

4 - The person who smokes Pall Mall keeps a bird.
4 - The person who smokes Blend lives next to the person who keeps a cat.
4 - The person who keeps a horse lives next to the person who smokes Dunhill.
4 - The person who smokes Blue Master drinks beer.
4 - The German smokes Prince.
4 - The man who smokes Blend has a neighbour who drinks water.

5 - The Brit lives in the red house.
5 - The green house is on the left side of the white house (NEXT TO IT)
5 - The person in the green house drinks coffee.
5 - The person in the yellow house smokes Dunhill.
5 - The Norwegian lives next to the blue house.
*/

void printSolution()
{
  int i;

  for(i = 0; i < 5; i++)
  {
    printf("%d\t", i);
    printf("%14s", nationalities[person[i]]);
    printf("%14s", pets[pet[i]]);
    printf("%14s", drinks[drink[i]]);
    printf("%14s", smokes[smoke[i]]);
    printf("%14s\n", houses[house[i]]);
  }
}

#define HASNEIGHBOUR(a,b) (((a) == ((b)-1)) || ((a) == ((b)+1)))

int find(int array[], int key)
{
  if(array[0] == key) return 0;
  if(array[1] == key) return 1;
  if(array[2] == key) return 2;
  if(array[3] == key) return 3;
  return 4;
}

void loopHouses()
{
  register int m1, m2, m3, m4, m5;

  for(m1 = 0; m1 < 5; m1++)
  {
    house[0] = m1;

    for(m2 = 0; m2 < 5; m2++)
    {
      if(m1 == m2) continue;

      house[1] = m2;

      for(m3 = 0; m3 < 5; m3++)
      {
        if(m1 == m3 || m2 == m3) continue;

        house[2] = m3;

        for(m4 = 0; m4 < 5; m4++)
        {
          if(m1 == m4 || m2 == m4 || m3 == m4) continue;

          house[3] = m4;

          for(m5 = 0; m5 < 5; m5++)
          {
            if(m1 == m5 || m2 == m5 || m3 == m5 || m4 == m5) continue;

            house[4] = m5;

            red = find(house, HOUSE_RED);
            green = find(house, HOUSE_GREEN);
            white = find(house, HOUSE_WHITE);
            yellow = find(house, HOUSE_YELLOW);
            blue = find(house, HOUSE_BLUE);

            if(british == red)
            {
              if(green == (white - 1))
              {
                if(green == coffee)
                {
                  if(yellow == dunhill)
                  {
                    if(HASNEIGHBOUR(norwegian, blue))
                    {
                      printSolution();
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

void loopSmokes()
{
  register int m1, m2, m3, m4, m5;

  for(m1 = 0; m1 < 5; m1++)
  {
    smoke[0] = m1;

    for(m2 = 0; m2 < 5; m2++)
    {
      if(m1 == m2) continue;

      smoke[1] = m2;

      for(m3 = 0; m3 < 5; m3++)
      {
        if(m1 == m3 || m2 == m3) continue;

        smoke[2] = m3;

        for(m4 = 0; m4 < 5; m4++)
        {
          if(m1 == m4 || m2 == m4 || m3 == m4) continue;

          smoke[3] = m4;

          for(m5 = 0; m5 < 5; m5++)
          {
            if(m1 == m5 || m2 == m5 || m3 == m5 || m4 == m5) continue;

            smoke[4] = m5;
 
            pallmall = find(smoke, SMOKE_PALLMALL);
            dunhill = find(smoke, SMOKE_DUNHILL);
            bluemaster = find(smoke, SMOKE_BLUEMASTER);
            blend = find(smoke, SMOKE_BLEND);
            prince = find(smoke, SMOKE_PRINCE);

            if(pallmall == bird)
            {
              if(HASNEIGHBOUR(blend, cat))
              {
                if(HASNEIGHBOUR(dunhill, horse))
                {
                  if(bluemaster == beer)
                  {
                    if(german == prince)
                    {
                      if(HASNEIGHBOUR(blend, water))
                      {
                        loopHouses();
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

void loopDrinks()
{
  register int m1, m2, m3, m4, m5;

  for(m1 = 0; m1 < 5; m1++)
  {
    drink[0] = m1;

    for(m2 = 0; m2 < 5; m2++)
    {
      if(m1 == m2) continue;

      drink[1] = m2;

      for(m3 = 0; m3 < 5; m3++)
      {
        if(m1 == m3 || m2 == m3) continue;

        drink[2] = m3;

        if(drink[2] == DRINK_MILK)
        {
          for(m4 = 0; m4 < 5; m4++)
          {
            if(m1 == m4 || m2 == m4 || m3 == m4) continue;

            drink[3] = m4;

            for(m5 = 0; m5 < 5; m5++)
            {
              if(m1 == m5 || m2 == m5 || m3 == m5 || m4 == m5) continue;

              drink[4] = m5;

              tea = find(drink, DRINK_TEA);
              coffee = find(drink, DRINK_COFFEE);
              milk = find(drink, DRINK_MILK);
              beer = find(drink, DRINK_BEER);
              water = find(drink, DRINK_WATER);

              if(danish == tea)
              {
                loopSmokes();
              }
            }
          }
        }
      }
    }
  }
}

void loopPets()
{
  register int m1, m2, m3, m4, m5;

  for(m1 = 0; m1 < 5; m1++)
  {
    pet[0] = m1;

    for(m2 = 0; m2 < 5; m2++)
    {
      if(m1 == m2) continue;

      pet[1] = m2;

      for(m3 = 0; m3 < 5; m3++)
      {
        if(m1 == m3 || m2 == m3) continue;

        pet[2] = m3;

        for(m4 = 0; m4 < 5; m4++)
        {
          if(m1 == m4 || m2 == m4 || m3 == m4) continue;

          pet[3] = m4;

          for(m5 = 0; m5 < 5; m5++)
          {
            if(m1 == m5 || m2 == m5 || m3 == m5 || m4 == m5) continue;

            pet[4] = m5;

            dog = find(pet, PET_DOG);
            cat = find(pet, PET_CAT);
            bird = find(pet, PET_BIRD);
            horse = find(pet, PET_HORSE);
            fish = find(pet, PET_FISH);
            
            if(swedish == dog)
            {
              loopDrinks();
            }
          }
        }
      }
    }
  }
}

void loopMen()
{
  register int m1, m2, m3, m4, m5;

  for(m1 = 0; m1 < 5; m1++)
  {
    person[0] = m1;

    if(person[0] == PERSON_NORWEGIAN)
    {
      for(m2 = 0; m2 < 5; m2++)
      {
        person[1] = m2;

        if(m1 == m2) continue;

        for(m3 = 0; m3 < 5; m3++)
        {
          if(m1 == m3 || m2 == m3) continue;

          person[2] = m3;

          for(m4 = 0; m4 < 5; m4++)
          {
            if(m1 == m4 || m2 == m4 || m3 == m4) continue;

            person[3] = m4;

            for(m5 = 0; m5 < 5; m5++)
            {
              if(m1 == m5 || m2 == m5 || m3 == m5 || m4 == m5) continue;

              person[4] = m5;

              british = find(person, PERSON_BRITISH);
              swedish = find(person, PERSON_SWEDISH);
              danish = find(person, PERSON_DANISH);
              norwegian = find(person, PERSON_NORWEGIAN);
              german = find(person, PERSON_GERMAN);

              loopPets();
            }
          }
        }
      }
    }
  }
}

int main(int argc, char **argv)
{
  int p1, p2, p3, p4, p5;
  int d1, d2, d3, d4, d5;
  int s1, s2, s3, s4, s5;
  int h1, h2, h3, h4, h5;

  loopMen();

  return 0;
}


