Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.02 KB | None | 0 0
  1. % Name: Tarik Abou-Saddik
  2. % ID: 27518722
  3. % Date: February 18, 2019
  4.  
  5. % Goal.
  6. numberlink((BoardSize, NumOfPaths, EndPoints)) :-
  7.     parse(EndPoints, Paths, Visited),
  8.     length(Paths, NumOfPaths),
  9.     map_paths(Paths,BoardSize,Visited,UpdatedPaths).
  10.     length(UpdatedPaths, NumOfPaths),
  11.     flatten(UpdatedPaths, X), length(X, FlatListLength),
  12.     ActualLength is FlatListLength - NumOfPaths, Area is BoardSize ^ 2,
  13.     ActualLength =:= Area,
  14.     format('~s~w,~w, ~n',["(",BoardSize, NumOfPaths]),
  15.     output_paths(UpdatedPaths).
  16.  
  17. % Output result in approrpiate format.
  18. output_paths([]).
  19. output_paths(Paths) :-
  20.     Paths = [[NumPath|Cells]|T],
  21.     format('~s~w~s ',["(",NumPath,":"]),
  22.     format_cells(Cells),!,
  23.     format('~s~n',[")"]),
  24.     output_paths(T),!.
  25.  
  26. % Format cells.
  27. format_cells([]).
  28. format_cells([Cell|Rest]) :-
  29.     Cell = cell(X,Y),
  30.     (Rest = [] ->
  31.         format('~s~w,~w~s',["(",X,Y,")"])
  32.         ;
  33.         format('~s~w,~w~s, ',["(",X,Y,")"])
  34.     ),
  35.     format_cells(Rest).
  36.  
  37. % Parse the given input.
  38. parse([],[],[]).
  39. parse(Cells, Paths, Visited) :-
  40.     Cells =.. [_|T],
  41.     (T = [(NumPath: (X1,Y1), X2,Y2) | [Rest]] ; T = [NumPath: (X1,Y1), (X2,Y2) | Rest]),
  42.     parse(Rest, RestOfPaths, RestOfVisited),!,
  43.     Paths = [path(NumPath,cell(X1, Y1), cell(X2,Y2)) | RestOfPaths],
  44.     Visited = [cell(X1,Y1), cell(X2,Y2) | RestOfVisited].
  45.  
  46. % Remove an item from a list.
  47. remove_item([Item|Tail], Item, List) :- List = Tail.
  48. remove_item([H|T], Item, List) :-
  49.     remove_item(T, Item, Tail),
  50.     List = [H|Tail].
  51.  
  52. % Move to the right.
  53. move(cell(X,Y), cell(X1,Y)) :- X1 is X + 1.
  54. % Move to the left.
  55. move(cell(X,Y),cell(X1,Y)) :- X1 is X - 1.
  56. % Move upward.
  57. move(cell(X,Y), cell(X,Y1)) :- Y1 is Y - 1.
  58. % Move downward.
  59. move(cell(X,Y), cell(X,Y1)) :- Y1 is Y + 1.
  60.  
  61. % Check to see if you're within the confines of the board.
  62. validCell(cell(X,Y), Limit) :- X < Limit + 1, Y < Limit + 1, X > 0, Y > 0.
  63.  
  64. % Map each Start/End cell entry in list connected_path/5 rule.
  65. map_paths([],_,_,[]).
  66. map_paths(Paths, BoardSize, Visited, NewPaths) :-
  67.     Paths = [path(NumPath,StartCell,EndCell) | Rest],
  68.     remove_item(Visited, EndCell, PathsVisited),
  69.     connected_path(BoardSize, StartCell, PathsVisited, NewVisited, EndCell),
  70.     append(Visited, NewVisited, X), sort(X, NewBoardLayout),
  71.     map_paths(Rest,BoardSize,NewBoardLayout,UpdatedPaths),
  72.     append([NumPath], NewVisited, Head),
  73.     NewPaths = [Head|UpdatedPaths],!.
  74.  
  75. % Distance between two cells.
  76. distance(cell(X1,Y1), cell(X2,Y2), Distance) :-
  77.     X is X2 - X1, Y is Y2 - Y1, Distance is abs(X) + abs(Y).
  78.  
  79. % Unifies with this once endpoint is reached.
  80. connected_path(_,EndCell,_,[EndCell],EndCell).
  81. connected_path(BoardSize, StartCell, PrevVisited, NewVisited, EndCell) :-
  82.     move(StartCell, NextCell),
  83.     validCell(NextCell, BoardSize),\+ member(NextCell, PrevVisited),
  84.     append([NextCell], PrevVisited, UpdatedVisited),
  85.     connected_path(BoardSize, NextCell, UpdatedVisited, NextVisited, EndCell),
  86.     NewVisited = [StartCell|NextVisited].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement