Advertisement
logicmoo

Untitled

May 16th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 8.55 KB | None | 0 0
  1.  
  2. -module( zebra_puzzle ).
  3.  
  4. -export( [task/0] ).
  5.  
  6. -record( house, {colour, drink, nationality, number, pet, smoke} ).
  7. -record( sorted_houses, {house_1s=[], house_2s=[], house_3s=[], house_4s=[], house_5s=[]} ).
  8.  
  9. task() ->
  10.     Houses = [#house{colour=C, drink=D, nationality=N, number=Nr, pet=P, smoke=S} || C <- all_colours(), D <- all_drinks(), N <- all_nationalities(), Nr <- all_numbers(), P <- all_pets(), S <- all_smokes(), is_all_single_house_rules_ok(C, D, N, Nr, P, S)],
  11.     Sorted_houses = lists:foldl( fun house_number_sort/2, #sorted_houses{}, Houses ),
  12.     Streets = [[H1, H2, H3, H4, H5] || H1 <- Sorted_houses#sorted_houses.house_1s, H2 <- Sorted_houses#sorted_houses.house_2s, H3 <- Sorted_houses#sorted_houses.house_3s, H4 <- Sorted_houses#sorted_houses.house_4s, H5 <- Sorted_houses#sorted_houses.house_5s, is_all_multi_house_rules_ok(H1, H2, H3, H4, H5)],
  13.     [Nationality] = [N || #house{nationality=N, pet=zebra} <- lists:flatten(Streets)],
  14.     io:fwrite( "~p owns the zebra~n", [Nationality] ),
  15.     io:fwrite( "All solutions ~p~n", [Streets] ),
  16.     io:fwrite( "Number of solutions ~p~n", [erlang:length(Streets)] ).
  17.  
  18.  
  19.  
  20. all_colours() -> [blue, green, red, white, yellow].
  21.  
  22. all_drinks() -> [beer, coffe, milk, tea, water].
  23.  
  24. all_nationalities() -> [danish, english, german, norveigan, swedish].
  25.  
  26. all_numbers() -> [1, 2, 3, 4, 5].
  27.  
  28. all_pets() -> [birds, cats, dog, horse, zebra].
  29.  
  30. all_smokes() -> [blend, 'blue master', dunhill, 'pall mall', prince].
  31.  
  32. house_number_sort( #house{number=1}=House, #sorted_houses{house_1s=Houses_1s}=Sorted_houses ) -> Sorted_houses#sorted_houses{house_1s=[House | Houses_1s]};
  33. house_number_sort( #house{number=2}=House, #sorted_houses{house_2s=Houses_2s}=Sorted_houses ) -> Sorted_houses#sorted_houses{house_2s=[House | Houses_2s]};
  34. house_number_sort( #house{number=3}=House, #sorted_houses{house_3s=Houses_3s}=Sorted_houses ) -> Sorted_houses#sorted_houses{house_3s=[House | Houses_3s]};
  35. house_number_sort( #house{number=4}=House, #sorted_houses{house_4s=Houses_4s}=Sorted_houses ) -> Sorted_houses#sorted_houses{house_4s=[House | Houses_4s]};
  36. house_number_sort( #house{number=5}=House, #sorted_houses{house_5s=Houses_5s}=Sorted_houses ) -> Sorted_houses#sorted_houses{house_5s=[House | Houses_5s]}.
  37.  
  38. is_all_different( [_H] ) -> true;
  39. is_all_different( [H | T] ) -> not lists:member( H, T ) andalso is_all_different( T ).
  40.  
  41. is_all_multi_house_rules_ok( House1, House2, House3, House4, House5 ) ->
  42.     is_rule_1_ok( House1, House2, House3, House4, House5 )
  43.     andalso is_rule_5_ok( House1, House2, House3, House4, House5 )
  44.     andalso is_rule_11_ok( House1, House2, House3, House4, House5 )
  45.     andalso is_rule_12_ok( House1, House2, House3, House4, House5 )
  46.     andalso is_rule_15_ok( House1, House2, House3, House4, House5 )
  47.     andalso is_rule_16_ok( House1, House2, House3, House4, House5 ).
  48.  
  49. is_all_single_house_rules_ok( Colour, Drink, Nationality, Number, Pet, Smoke ) ->
  50.     is_rule_ok( {rule_number, 2}, {Nationality, english}, {Colour, red})
  51.     andalso is_rule_ok( {rule_number, 3}, {Nationality, swedish}, {Pet, dog})
  52.     andalso is_rule_ok( {rule_number, 4}, {Nationality, danish}, {Drink, tea})
  53.     andalso is_rule_ok( {rule_number, 6}, {Drink, coffe}, {Colour, green})
  54.     andalso is_rule_ok( {rule_number, 7}, {Smoke, 'pall mall'}, {Pet, birds})
  55.     andalso is_rule_ok( {rule_number, 8}, {Colour, yellow}, {Smoke, dunhill})
  56.     andalso is_rule_ok( {rule_number, 9}, {Number, 3}, {Drink, milk})
  57.     andalso is_rule_ok( {rule_number, 10}, {Nationality, norveigan}, {Number, 1})
  58.     andalso is_rule_ok( {rule_number, 13}, {Smoke, 'blue master'}, {Drink, beer})
  59.     andalso is_rule_ok( {rule_number, 14}, {Nationality, german}, {Smoke, prince}).
  60.  
  61. is_rule_ok( _Rule_number, {A, A}, {B, B} ) -> true;
  62. is_rule_ok( _Rule_number, _A, {B, B} ) -> false;
  63. is_rule_ok( _Rule_number, {A, A}, _B ) -> false;
  64. is_rule_ok( _Rule_number, _A, _B ) -> true.
  65.  
  66. is_rule_1_ok( #house{number=1}=H1,  #house{number=2}=H2,  #house{number=3}=H3,  #house{number=4}=H4,  #house{number=5}=H5  ) ->
  67.     is_all_different( [H1#house.colour, H2#house.colour, H3#house.colour, H4#house.colour, H5#house.colour] )
  68.     andalso is_all_different( [H1#house.drink, H2#house.drink, H3#house.drink, H4#house.drink, H5#house.drink] )
  69.     andalso is_all_different( [H1#house.nationality, H2#house.nationality, H3#house.nationality, H4#house.nationality, H5#house.nationality] )
  70.     andalso is_all_different( [H1#house.pet, H2#house.pet, H3#house.pet, H4#house.pet, H5#house.pet] )
  71.     andalso is_all_different( [H1#house.smoke, H2#house.smoke, H3#house.smoke, H4#house.smoke, H5#house.smoke] );
  72. is_rule_1_ok( _House1,  _House2,  _House3,  _House4,  _House5  ) -> false.
  73.  
  74. is_rule_5_ok( #house{colour=green},  #house{colour=white},  _House3,  _House4,  _House5  ) -> true;
  75. is_rule_5_ok( _House1,  #house{colour=green},  #house{colour=white},  _House4,  _House5  ) -> true;
  76. is_rule_5_ok( _House1,  _House2,  #house{colour=green},  #house{colour=white},  _House5  ) -> true;
  77. is_rule_5_ok( _House1,  _House2,  _House3,  #house{colour=green},  #house{colour=white}  ) -> true;
  78. is_rule_5_ok( _House1,  _House2,  _House3,  _House4,  _House5  ) -> false.
  79.  
  80. is_rule_11_ok( #house{smoke=blend},  #house{pet=cats},  _House3,  _House4,  _House5  ) -> true;
  81. is_rule_11_ok( _House1,  #house{smoke=blend},  #house{pet=cats},  _House4,  _House5  ) -> true;
  82. is_rule_11_ok( _House1,  _House2,  #house{smoke=blend},  #house{pet=cats},  _House5  ) -> true;
  83. is_rule_11_ok( _House1,  _House2,  _House3,  #house{smoke=blend},  #house{pet=cats}  ) -> true;
  84. is_rule_11_ok( #house{pet=cats},  #house{smoke=blend},  _House3,  _House4,  _House5  ) -> true;
  85. is_rule_11_ok( _House1,  #house{pet=cats},  #house{smoke=blend},  _House4,  _House5  ) -> true;
  86. is_rule_11_ok( _House1,  _House2,  #house{pet=cats},  #house{smoke=blend},  _House5  ) -> true;
  87. is_rule_11_ok( _House1,  _House2,  _House3,  #house{pet=cats},  #house{smoke=blend}  ) -> true;
  88. is_rule_11_ok( _House1,  _House2,  _House3,  _House4,  _House5  ) -> false.
  89.  
  90. is_rule_12_ok( #house{smoke=dunhill},  #house{pet=horse},  _House3,  _House4,  _House5  ) -> true;
  91. is_rule_12_ok( _House1,  #house{smoke=dunhill},  #house{pet=horse},  _House4,  _House5  ) -> true;
  92. is_rule_12_ok( _House1,  _House2,  #house{smoke=dunhill},  #house{pet=horse},  _House5  ) -> true;
  93. is_rule_12_ok( _House1,  _House2,  _House3,  #house{smoke=dunhill},  #house{pet=horse}  ) -> true;
  94. is_rule_12_ok( #house{pet=horse},  #house{smoke=dunhill},  _House3,  _House4,  _House5  ) -> true;
  95. is_rule_12_ok( _House1,  #house{pet=horse},  #house{smoke=dunhill},  _House4,  _House5  ) -> true;
  96. is_rule_12_ok( _House1,  _House2,  #house{pet=horse},  #house{smoke=dunhill},  _House5  ) -> true;
  97. is_rule_12_ok( _House1,  _House2,  _House3,  #house{pet=horse},  #house{smoke=dunhill}  ) -> true;
  98. is_rule_12_ok( _House1,  _House2,  _House3,  _House4,  _House5  ) -> false.
  99.  
  100. is_rule_15_ok( #house{nationality=norveigan},  #house{colour=blue},  _House3,  _House4,  _House5  ) -> true;
  101. is_rule_15_ok( _House1,  #house{nationality=norveigan},  #house{colour=blue},  _House4,  _House5  ) -> true;
  102. is_rule_15_ok( _House1,  _House2,  #house{nationality=norveigan},  #house{colour=blue},  _House5  ) -> true;
  103. is_rule_15_ok( _House1,  _House2,  _House3,  #house{nationality=norveigan},  #house{colour=blue}  ) -> true;
  104. is_rule_15_ok( #house{colour=blue},  #house{nationality=norveigan},  _House3,  _House4,  _House5  ) -> true;
  105. is_rule_15_ok( _House1,  #house{colour=blue},  #house{nationality=norveigan},  _House4,  _House5  ) -> true;
  106. is_rule_15_ok( _House1,  _House2,  #house{drink=water},  #house{nationality=norveigan},  _House5  ) -> true;
  107. is_rule_15_ok( _House1,  _House2,  _House3,  #house{drink=water},  #house{nationality=norveigan}  ) -> true;
  108. is_rule_15_ok( _House1,  _House2,  _House3,  _House4,  _House5  ) -> false.
  109.  
  110. is_rule_16_ok( #house{smoke=blend},  #house{drink=water},  _House3,  _House4,  _House5  ) -> true;
  111. is_rule_16_ok( _House1,  #house{smoke=blend},  #house{drink=water},  _House4,  _House5  ) -> true;
  112. is_rule_16_ok( _House1,  _House2,  #house{smoke=blend},  #house{drink=water},  _House5  ) -> true;
  113. is_rule_16_ok( _House1,  _House2,  _House3,  #house{smoke=blend},  #house{drink=water}  ) -> true;
  114. is_rule_16_ok( #house{drink=water},  #house{smoke=blend},  _House3,  _House4,  _House5  ) -> true;
  115. is_rule_16_ok( _House1,  #house{drink=water},  #house{smoke=blend},  _House4,  _House5  ) -> true;
  116. is_rule_16_ok( _House1,  _House2,  #house{drink=water},  #house{smoke=blend},  _House5  ) -> true;
  117. is_rule_16_ok( _House1,  _House2,  _House3,  #house{drink=water},  #house{smoke=blend}  ) -> true;
  118. is_rule_16_ok( _House1,  _House2,  _House3,  _House4,  _House5  ) -> false.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement