improve d8p2 readability
This commit is contained in:
parent
1a7326555a
commit
a997d00c89
64
08/part2.pl
64
08/part2.pl
@ -1,5 +1,3 @@
|
|||||||
:- table direction_loop/1.
|
|
||||||
|
|
||||||
:- op(700, xfx, l).
|
:- op(700, xfx, l).
|
||||||
:- op(700, xfx, r).
|
:- op(700, xfx, r).
|
||||||
From l To :- From to To-_.
|
From l To :- From to To-_.
|
||||||
@ -14,51 +12,55 @@ collapse_routes([X], X).
|
|||||||
collapse_routes(Routes, Collapsed) :-
|
collapse_routes(Routes, Collapsed) :-
|
||||||
writef('Collapsing: %t\n', [Routes]),
|
writef('Collapsing: %t\n', [Routes]),
|
||||||
sort(2, @=<, Routes, [Route1, Route2 | CdrRoutes]),
|
sort(2, @=<, Routes, [Route1, Route2 | CdrRoutes]),
|
||||||
unify2(Route1, Route2, 0-0-0-0, [], NewRoute),
|
unify2(Route1, Route2, NewRoute),
|
||||||
collapse_routes([NewRoute | CdrRoutes], Collapsed).
|
collapse_routes([NewRoute | CdrRoutes], Collapsed).
|
||||||
|
|
||||||
unify2(Route1, Route2, N1-Term1-N2-Term2, Founds, NewRoute) :-
|
% unify2 combines 2 routes into one with its own stride-offsets-dests
|
||||||
Route1 = A1-(Stride1-Offset1-Loop1),
|
unify2(Route1, Route2, Route12) :- unify2(Route1, Route2, 0-0-0-0, [], Route12).
|
||||||
Route2 = A2-(Stride2-Offset2-Loop2),
|
unify2(Route1, Route2, N1-Dest1-N2-Dest2, Founds, NewRoute) :-
|
||||||
nth0(Term1, Loop1, C1-_),
|
Route1 = A1-(Stride1-Offset1-Dests1),
|
||||||
nth0(Term2, Loop2, C2-_),
|
Route2 = A2-(Stride2-Offset2-Dests2),
|
||||||
Z1 is Offset1 + Stride1*N1 + C1,
|
nth0(Dest1, Dests1, C1-Z1),
|
||||||
Z2 is Offset2 + Stride2*N2 + C2,
|
nth0(Dest2, Dests2, C2-Z2),
|
||||||
next(Route1, N1-Term1, NextN1-NextTerm1),
|
Len1 is Offset1 + Stride1*N1 + C1,
|
||||||
( Z1 =:= Z2
|
Len2 is Offset2 + Stride2*N2 + C2,
|
||||||
-> ( Founds = [OldZ]
|
next(Dests1, N1-Dest1, NextN1-NextDest1),
|
||||||
-> atom_concat(A1, A2, NewA),
|
( Len1 =:= Len2
|
||||||
NewStride is Z1 - OldZ,
|
-> ( Founds = [OldLen]
|
||||||
NewRoute = NewA-(NewStride-OldZ-[0-NewA]),
|
-> atom_concat(A1, A2, NewA), atom_concat(Z1, Z2, NewZ),
|
||||||
|
NewStride is Len1 - OldLen,
|
||||||
|
NewRoute = NewA-(NewStride-OldLen-[0-NewZ]),
|
||||||
!
|
!
|
||||||
; unify2(Route1, Route2, NextN1-NextTerm1-N2-Term2, [Z1], NewRoute)
|
; unify2(Route1, Route2, NextN1-NextDest1-N2-Dest2, [Len1], NewRoute)
|
||||||
)
|
)
|
||||||
; Z1 < Z2
|
; Len1 < Len2
|
||||||
-> unify2(Route1, Route2, NextN1-NextTerm1-N2-Term2, Founds, NewRoute)
|
-> unify2(Route1, Route2, NextN1-NextDest1-N2-Dest2, Founds, NewRoute)
|
||||||
; next(Route2, N2-Term2, NextN2-NextTerm2),
|
; next(Dests2, N2-Dest2, NextN2-NextDest2),
|
||||||
unify2(Route1, Route2, N1-Term1-NextN2-NextTerm2, Founds, NewRoute)
|
unify2(Route1, Route2, N1-Dest1-NextN2-NextDest2, Founds, NewRoute)
|
||||||
).
|
).
|
||||||
|
|
||||||
next(_-(_-_-Loop), NLoops-Term, NextNLoops-NextTerm) :-
|
% next(Dests, NumberOfLoops-WhichInternalZ, NextNumberOfLoops-NextInternalZ)
|
||||||
length(Loop, LoopLen),
|
next(Dests, NLoops-Dest, NextNLoops-NextDest) :-
|
||||||
( Term < LoopLen - 1
|
length(Dests, DestsLen),
|
||||||
-> NextNLoops is NLoops, NextTerm is Term + 1
|
( Dest < DestsLen - 1
|
||||||
; NextNLoops is NLoops + 1, NextTerm is 0).
|
-> NextNLoops is NLoops, NextDest is Dest + 1
|
||||||
|
; NextNLoops is NLoops + 1, NextDest is 0).
|
||||||
|
|
||||||
routes(Starts, Routes) :-
|
routes(Starts, Routes) :-
|
||||||
maplist([S, S-Route]>>(zloop([], S, 0, [], Route)), Starts, Routes).
|
maplist([S, S-Route]>>(zloop(S, Route)), Starts, Routes).
|
||||||
|
|
||||||
zloop(_, _, _, Zs, Stride-Offset-Loop) :-
|
% zloop builds a route (Stride-Offset-Internals for a particular starting node.
|
||||||
|
zloop(Node, Route) :- zloop([], Node, 0, [], Route).
|
||||||
|
zloop(_Direction, _Node, _Index, Zs, Stride-Offset-Dests) :-
|
||||||
Zs = [FirstZIndex-Z | _], reverse(Zs, [LastZIndex-Z | ReversedZs]),
|
Zs = [FirstZIndex-Z | _], reverse(Zs, [LastZIndex-Z | ReversedZs]),
|
||||||
DeltaZ is LastZIndex - FirstZIndex, DeltaZ =\= 0,
|
DeltaZ is LastZIndex - FirstZIndex, DeltaZ =\= 0,
|
||||||
direction_len(Len),
|
direction_len(Len),
|
||||||
divmod(DeltaZ, Len, _, 0),
|
divmod(DeltaZ, Len, _, 0),
|
||||||
Offset = FirstZIndex, Stride = DeltaZ,
|
Offset = FirstZIndex, Stride = DeltaZ,
|
||||||
foldl([Idx-Z, NewIdx-Z, Off, Off]>>(NewIdx is Idx - Off),
|
foldl([Idx-Z, NewIdx-Z, Off, Off]>>(NewIdx is Idx - Off),
|
||||||
ReversedZs, ReversedLoop, Offset, _),
|
ReversedZs, ReversedDests, Offset, _),
|
||||||
reverse(ReversedLoop, Loop),
|
reverse(ReversedDests, Dests),
|
||||||
!.
|
!.
|
||||||
|
|
||||||
zloop(Directions, Node, Index, Zs, Route) :-
|
zloop(Directions, Node, Index, Zs, Route) :-
|
||||||
( is_end(Node)
|
( is_end(Node)
|
||||||
-> append(Zs, [Index-Node], NewZs)
|
-> append(Zs, [Index-Node], NewZs)
|
||||||
|
Loading…
Reference in New Issue
Block a user