-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.pi
25 lines (20 loc) · 827 Bytes
/
part2.pi
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
import util.
main =>
G = new_map(),
[Moves,_|GraphS] = read_file_lines(),
extract(GraphS,G),
Starts = [Node : Node in G.keys, Node = [_,_,'A']],
Counts = [proceed(Start, Moves, Moves, G, 0) : Start in Starts],
println(lcm(Counts)).
lcm([A]) = A.
lcm([A,B|Xs]) = lcm([A*B // gcd(A,B) | Xs]).
proceed([_,_,'Z'], _, _, _, Count) = Count.
proceed(Here, [], Moves, G, Count) = proceed(Here, Moves, Moves, G, Count).
proceed(Here, ['L'|Steps], Moves, G, Count) = R, Options = G.get(Here) => R = proceed(Options[1], Steps, Moves, G, Count + 1).
proceed(Here, ['R'|Steps], Moves, G, Count) = R, Options = G.get(Here) => R = proceed(Options[2], Steps, Moves, G, Count + 1).
extract([],M).
extract([""|Xs],M) :- extract(Xs,M).
extract([S|Xs],M) :-
[Here,L,R] = S.split(" =(,)"),
M.put(Here, {L,R}),
extract(Xs,M).