-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.pl
67 lines (58 loc) · 1.27 KB
/
maze.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
connected(A, B, [Next]) :-
directly_connected(A,B),
Next = B.
connected(A, B, [Next]) :-
directly_connected(B,A),
Next = B.
connected(A, B, [Next|Tail]) :-
directly_connected(A, C),
Next = C,
connected(C, B, Tail).
connected(A, B, [Next|Tail]) :-
directly_connected(C, A),
Next = C,
connected(C, B, Tail).
list_contains([], _) :-
1 = 2.
list_contains([A], B) :-
A = B.
list_contains([Ah|_], B) :-
Ah = B.
list_contains([_|At], B) :-
list_contains(At, B).
list_lacks([], _).
list_lacks([A], B) :-
A \= B.
list_lacks([Ah|At], B) :-
Ah \= B,
list_lacks(At, B).
left_has_right(_, []).
left_has_right(L, [R]) :-
list_contains(L, R).
left_has_right(L, R) :-
R = [Rh|Rt],
list_contains(L, Rh),
left_has_right(L, Rt).
lists_equal([], []).
lists_equal(A, B) :-
left_has_right(A,B),
left_has_right(B,A).
no_duplicates([]).
no_duplicates([_]).
no_duplicates([out|T]) :-
no_duplicates(T).
no_duplicates(L) :-
L = [Lh|Lt],
list_lacks(Lt, Lh),
no_duplicates(Lt).
path(Required, Path) :-
Start = Required,
path(Required, Start, Path).
path(Required, Start, Path) :-
Path = [_|Pt],
Start = [St|_],
connected(St, _, Pt),
no_duplicates(Path),
left_has_right(Path, Required).
path(Required, [_|Start], Path) :-
path(Required, Start, Path).