#include char BRITISH = '0'; char SWEEDISH = '1'; char DANISH = '2'; char NORWEGIAN = '3'; char GERMAN = '4'; char RED = '0'; char BLUE = '1'; char YELLOW = '2'; char GREEN = '3'; char WHITE = '4'; char TEA = '0'; char COFFEE = '1'; char MILK = '2'; char BEER = '3'; char WATER = '4'; char BLENDS = '0'; char PALL = '1'; char DUNHILL = '2'; char PRINCE = '3'; char BLUE_MASTER = '4'; char HORSES = '0'; char BIRDS = '1'; char DOGS = '2'; char CATS = '3'; char FISH = '4'; #define bool int #define false 0; #define true 1; bool Check(char color[]) { int i; bool found = false; for (i = 0; i < 5; i++) { if (i <4 && color[i] == GREEN && color[i + 1] == WHITE) found = true; if (i == 4 && !found) return false; } return true; } bool Check5(char color[], char nationality[], char beverage[], char cigar[], char pet[]) { int i; for (i = 0; i < 5; i++) { if (nationality[i] == SWEEDISH && pet[i] != DOGS) return false; if (cigar[i] == PALL && pet[i] != BIRDS) return false; if (color[i] == YELLOW && cigar[i] != DUNHILL) return false; //if (cigar[i] == BLENDS && !nextTo(i, I => pet[I] == CATS)) return false; //if (pet[i] == HORSES && !nextTo(i, I => cigar[I] == DUNHILL)) return false; if (cigar[i] == BLENDS) { bool found = false; if(i+1<5 && pet[i+1] == CATS) found = true; if(i-1>=0 && pet[i-1] == CATS) found = true; if(!found) return false; } if (pet[i] == HORSES) { bool found = false; if(i+1<5 && cigar[i+1] == DUNHILL) found = true; if(i-1>=0 && cigar[i-1] == DUNHILL) found = true; if(!found) return false; } } return true; } /* bool nextTo(int i, Func func) { if(i+1 < 5 && func(i+1)) return true; if (i - 1 >= 0 && func(i - 1)) return true; return false; }*/ bool Check4(char color[], char nationality[], char beverage[], char cigar[]) { int i; for (i = 0; i < 5; i++) { if (nationality[i] == BRITISH && color[i] != RED) return false; if (cigar[i] == BLUE_MASTER && beverage[i]!=BEER) return false; if (nationality[i] == GERMAN && cigar[i] != PRINCE) return false; //if (cigar[i] == BLENDS && !nextTo(i, I => beverage[I] == WATER)) return false; if (cigar[i] == BLENDS) { bool found = false; if(i+1<5 && beverage[i+1] == WATER) found = true; if(i-1>=0 && beverage[i-1] == WATER) found = true; if(!found) return false; } } return true; } bool Check3(char color[], char nationality[], char beverage[]) { int i; //display(new string(color) + " " + new string(nationality) + " " + new string(beverage)); for (i = 0; i < 5; i++) { if (nationality[i] == DANISH && beverage[i] != TEA) return false; if (color[i] == GREEN && beverage[i] != COFFEE) return false; if (i == 2 && beverage[i] != MILK) return false; } return true; } bool Check2(char color[], char nationality[]) { int i; for (i = 0; i < 5; i++) { if (nationality[i] == BRITISH && color[i] != RED) return false; if (nationality[i] == NORWEGIAN && i!=0) return false; //if (nationality[i] == NORWEGIAN && !nextTo(i, I => color[I] == BLUE)) return false; if (nationality[i] == NORWEGIAN) { bool found = false; if(i+1<5 && color[i+1] == BLUE) found = true; if(i-1>=0 && color[i-1] == BLUE) found = true; if(!found) return false; } } return true; } char* rotate(char *s, int pos) { int start = 5 -1 - pos; char ch = s[start]; int i; for (i = start; i < 5 - 1; i++) { s[i] = s[i + 1]; } s[5 - 1] = ch; return s; } char* permute(char *s, int pos) { if (pos == 0) return s; // Console.Write(new string(s)); rotate(s, 1); // Console.Write(" -> " + new string(s)); if (pos % 2 == 0) { rotate(s, 2); // display( " -> " + new string(s)); } // else display(); if (pos % 6 == 0) rotate(s, 3); if (pos % 24 == 0) rotate(s, 4); return s; } void display(char * str) { printf(str); printf("\n"); } void main() { char _color[] = "01234"; char _nationality[] = "01234"; char _beverage[] = "01234"; char _cigar[] = "01234"; char _pet[] = "01234"; int color, nationality, beverage, cigar,pet; for (color = 0; color < 120; color++) { permute(_color, color); //display(color); if (!Check(_color)) continue; for (nationality = 0; nationality < 120; nationality++) { permute(_nationality, nationality); if (!Check2(_color, _nationality)) continue; for (beverage = 0; beverage < 120; beverage++) { permute(_beverage, beverage); if (!Check3(_color, _nationality, _beverage)) continue; //display(new string(_color) + " " + new string(_nationality) + " " + new string(_beverage)); for (cigar = 0; cigar < 120; cigar++) { permute(_cigar, cigar); if (!Check4(_color, _nationality, _beverage, _cigar)) continue; for (pet = 0; pet < 120; pet++) { permute(_pet, pet); if (!Check5(_color, _nationality, _beverage, _cigar, _pet)) continue; display(_nationality); display(_color); display(_beverage); display(_cigar); display(_pet); } } } } } }