SHARE
TWEET

Prolog Problem_Q

a guest Apr 21st, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % Data sets
  2. % a(Column, Row, Profit)
  3. % b(Column, Row, Profit)   
  4.    
  5. a(65, 66, -0.82).
  6. a(65, 65.5, -1.32).
  7. a(65, 65, -1.82).
  8.  
  9. a(65.5, 66, -1.07).
  10. a(65.5, 65.5, -1.57).
  11. a(65.5, 65, -1.57).
  12.  
  13. a(66, 66, -1.3).
  14. a(66, 65.5, -1.3).
  15. a(66, 65, -1.3).
  16.  
  17. b(65, 66, -1).
  18. b(65, 65.5, -0.5).
  19. b(65, 65, 1.72).
  20.  
  21. b(65.5, 66, -0.5).
  22. b(65.5, 65.5, 1.48).
  23. b(65.5, 65, 1.48).
  24.  
  25. b(66, 66, 1.25).
  26. b(66, 65.5, 1.25).
  27. b(66, 65, 1.25).
  28.  
  29. min_row(Row) :-
  30.     a(Col, Row, _),
  31.     \+ (a(_,Row2,_), Row2 < Row),!.
  32.    
  33. max_row(Row) :-
  34.     a(Col, Row, _),
  35.     \+ (a(_,Row2,_), Row2 > Row),!.
  36.  
  37. is_profit(ColA, ColB, Row, P) :-
  38.     a(ColA, Row, Profit1),
  39.     b(ColB, Row, Profit2),
  40.     Profit is Profit1 + Profit2,
  41.     ( Profit > 0 -> P is 1 ; P is 0),!.
  42.    
  43. count_profits(ColA, ColB, Row1, P) :-
  44.     max_row(Row),
  45.     Row1 =:= Row,
  46.     is_profit(ColA, ColB, Row1, P).
  47.  
  48. count_profits(ColA, ColB, Row1, P) :-
  49.     a(ColA,Row2,_),
  50.     Row2 > Row1,
  51.     count_profits(ColA, ColB, Row2, P2),
  52.     is_profit(ColA, ColB, Row1, P1),
  53.     P is P1+P2.
  54.    
  55. count_profits(ColA, ColB, P) :-
  56.     min_row(Row1),
  57.     count_profits(ColA, ColB, Row1, P),!.
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top