2023-12-08 22:21:08 -06:00
|
|
|
:- op(700, xfx, l).
|
|
|
|
:- op(700, xfx, r).
|
|
|
|
From l To :- From to To-_.
|
|
|
|
From r To :- From to _-To.
|
|
|
|
|
|
|
|
answer(Answer) :-
|
|
|
|
starts(Starts),
|
2023-12-09 18:41:07 -06:00
|
|
|
routes(Starts, [Route1 | RestOfRoutes]),
|
|
|
|
foldl(unify2, RestOfRoutes, Route1, _-(_-Answer-_)).
|
2023-12-08 22:21:08 -06:00
|
|
|
|
2023-12-09 13:41:52 -06:00
|
|
|
% unify2 combines 2 routes into one with its own stride-offsets-dests
|
2023-12-09 18:41:07 -06:00
|
|
|
unify2(Route1, Route2, NewRoute) :-
|
2023-12-10 13:30:50 -06:00
|
|
|
writef('Combining %t + %t', [Route1, Route2]),
|
2023-12-09 18:41:07 -06:00
|
|
|
once(findnsols(2, S, converge(Route1, Route2, S), SolutionPair)),
|
|
|
|
SolutionPair = [NewA-LenA-NewZ, _-LenB-_],
|
|
|
|
NewStride is LenB - LenA,
|
2023-12-10 13:30:50 -06:00
|
|
|
NewRoute = NewA-(NewStride-LenA-[0-NewZ]),
|
|
|
|
writef(' --> %t\n', [NewRoute]).
|
2023-12-09 18:41:07 -06:00
|
|
|
|
|
|
|
% Len = Stride1*X1 + Offset1 + Dest1 = Stride2*X2 + Offset2 + Dest2
|
|
|
|
% For performance, Route1's Stride should =< Route2's Stride
|
|
|
|
converge(Route1, Route2, NewA-Len-NewZ) :-
|
2023-12-09 13:41:52 -06:00
|
|
|
Route1 = A1-(Stride1-Offset1-Dests1),
|
|
|
|
Route2 = A2-(Stride2-Offset2-Dests2),
|
2023-12-09 18:41:07 -06:00
|
|
|
natnum(X2),
|
|
|
|
pick([Dests1, Dests2], [Dest1-Z1, Dest2-Z2]),
|
|
|
|
0 is (Stride2*X2 + Offset2 + Dest2 - Offset1 - Dest1) mod Stride1,
|
|
|
|
Len is Stride2*X2 + Offset2 + Dest2,
|
|
|
|
atom_concat(A1, A2, NewA), atom_concat(Z1, Z2, NewZ).
|
2023-12-08 22:21:08 -06:00
|
|
|
|
|
|
|
routes(Starts, Routes) :-
|
2023-12-10 13:30:50 -06:00
|
|
|
maplist([S, S-Route]>>(route_at(S, Route)), Starts, Routes).
|
2023-12-08 22:21:08 -06:00
|
|
|
|
2023-12-10 13:30:50 -06:00
|
|
|
% route_at(N, Node, Dests, AllDests) means going N steps will arrive at Node
|
|
|
|
% passing through Dests.
|
|
|
|
route_at(Start, Route) :- route_at(0, Start, [], Route).
|
|
|
|
route_at(N, FirstZ, [FirstZN-FirstZ | Zs], Stride-Offset-AllZs) :-
|
|
|
|
Stride is N - FirstZN, direction_looped(Stride),
|
|
|
|
Offset is FirstZN,
|
|
|
|
maplist({Offset}/[N-X, M-X]>>(M is N-Offset), [FirstZN-FirstZ | Zs], AllZs),
|
2023-12-08 22:21:08 -06:00
|
|
|
!.
|
2023-12-10 13:30:50 -06:00
|
|
|
route_at(N, At, Dests, AllDests) :-
|
|
|
|
( is_end(At)
|
|
|
|
-> append(Dests, [N-At], NextDests)
|
|
|
|
; NextDests = Dests),
|
|
|
|
Nplus1 is N + 1, step_at(N, Step), call(Step, At, NextNode),
|
|
|
|
route_at(Nplus1, NextNode, NextDests, AllDests).
|
2023-12-08 22:21:08 -06:00
|
|
|
|
|
|
|
starts(Starts) :- findall(X, X to _, Nodes), include(is_start, Nodes, Starts).
|
|
|
|
|
|
|
|
is_start(Node) :- atom_chars(Node, [_, _, a]).
|
|
|
|
is_end(Node) :- atom_chars(Node, [_, _, z]).
|
|
|
|
|
2023-12-10 13:30:50 -06:00
|
|
|
% Step is the N-th step (counting starts from 0).
|
|
|
|
step_at(N, Step) :-
|
|
|
|
direction_list(Dir), length(Dir, DirLen), Remainder is N mod DirLen,
|
|
|
|
nth0(Remainder, Dir, Step).
|
2023-12-08 22:21:08 -06:00
|
|
|
|
|
|
|
direction_list(Dir) :- direction(Str), atom_chars(Str, Dir).
|
2023-12-10 13:30:50 -06:00
|
|
|
direction_looped(Len) :- direction_list(D), length(D, DLen), 0 is Len mod DLen.
|
2023-12-09 18:41:07 -06:00
|
|
|
|
2023-12-10 13:30:50 -06:00
|
|
|
% pick one item from each sublist of ListOfLists & put them into Items in order.
|
|
|
|
% [[1,2,3], [4], [5,6]] -> [1,4,5]; [1,4,6]; [2,4,5]; [2,4,6]; [3,4,5]; [3,4,6].
|
2023-12-09 18:41:07 -06:00
|
|
|
pick(ListOfLists, Items) :-
|
|
|
|
maplist([SubList, X]>>(member(X, SubList)), ListOfLists, Items).
|
|
|
|
|
|
|
|
natnum(0).
|
|
|
|
natnum(N) :- natnum(N0), N is N0 + 1.
|