#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];

// 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.
*/

int checkSolution()
{
  return 0;
}

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]]);
  }
}

int hasNeighbour(int array[], int key, int narray[], int nkey)
{
  int house = find(array, key);
  int neighbour = find(narray, nkey);

  return ((neighbour == house - 1) || (neighbour == house + 1)) ? 1 : 0;
}

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++)
  {
    for(m2 = 0; m2 < 5; m2++)
    {
      if(m1 == m2) continue;

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

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

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

            house[0] = m1;
            house[1] = m2;
            house[2] = m3;
            house[3] = m4;
            house[4] = m5;

            if(person[0] == PERSON_NORWEGIAN)
            {
              if(find(person, PERSON_SWEDISH) == find(pet, PET_DOG))
              {
                if(drink[2] == DRINK_MILK)
                {
                  if(find(person, PERSON_DANISH) == find(drink, DRINK_TEA))
                  {
                    if(find(smoke, SMOKE_PALLMALL) == find(pet, PET_BIRD))
                    {
                      if(hasNeighbour(smoke, SMOKE_BLEND, pet, PET_CAT))
                      {
                        if(hasNeighbour(smoke, SMOKE_DUNHILL, pet, PET_HORSE))
                        {
                          if(find(smoke, SMOKE_BLUEMASTER) == find(drink, DRINK_BEER))
                          {
                            if(find(person, PERSON_GERMAN) == find(smoke, SMOKE_PRINCE))
                            {
                              if(hasNeighbour(smoke, SMOKE_BLEND, drink, DRINK_WATER))
                              {
                                if(find(person, PERSON_BRITISH) == find(house, HOUSE_RED))
                                {
                                  if(find(house, HOUSE_GREEN) == (find(house, HOUSE_WHITE) - 1))
                                  {
                                    if(find(house, HOUSE_GREEN) == find(drink, DRINK_COFFEE))
                                    {
                                      if(find(house, HOUSE_YELLOW) == find(smoke, SMOKE_DUNHILL))
                                      {
                                        if(hasNeighbour(person, PERSON_NORWEGIAN, house, HOUSE_BLUE))
                                        {
                                          printSolution();
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

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

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

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

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

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

            smoke[0] = m1;
            smoke[1] = m2;
            smoke[2] = m3;
            smoke[3] = m4;
            smoke[4] = m5;
 
            loopHouses();
          }
        }
      }
    }
  }
}

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

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

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

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

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

            drink[0] = m1;
            drink[1] = m2;
            drink[2] = m3;
            drink[3] = m4;
            drink[4] = m5;

            loopSmokes();
          }
        }
      }
    }
  }
}

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

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

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

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

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

            pet[0] = m1;
            pet[1] = m2;
            pet[2] = m3;
            pet[3] = m4;
            pet[4] = m5;

            loopDrinks();
          }
        }
      }
    }
  }
}

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

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

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

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

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

            person[0] = m1;
            person[1] = m2;
            person[2] = m3;
            person[3] = m4;
            person[4] = m5;

            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;
}


