improved d11 implementation

This commit is contained in:
Duy Truong 2023-12-11 17:16:18 -08:00
parent 6707722fa8
commit 0534215429
2 changed files with 36 additions and 42 deletions

View File

@ -1,34 +1,31 @@
:- use_module(library(pio)). :- use_module(library(pio)).
:- initialization(main, main). :- initialization(main, main).
:- table galaxies/3.
main([FileName | _]) :- main([FileName | _]) :-
input(FileName, Map), input(FileName, Map),
findall(Dist, findall(Dist, distance(_, Map, _, Dist), Dists),
( galaxies(Axis, Map, A-ACount), ACount =\= 0,
galaxies(Axis, Map, B-BCount), BCount =\= 0,
A < B,
distance(Axis, Map, A, B, DistPair),
Dist is DistPair * ACount * BCount),
Dists),
% Sum of manhattan distances is sum of distances on all axes.
sum_list(Dists, Sum), sum_list(Dists, Sum),
writef('Answer=%t\n', [Sum]). writef('Answer=%t\n', [Sum]).
distance(Axis, Map, X1, X2, Dist) :- distance(Axis, Map, A-B, Dist) :-
true_coord(Axis, Map, X1, TrueX1), true_coord(Axis, Map, X2, TrueX2), true_coords(Axis, Map, TrueNs),
Dist is abs(TrueX1 - TrueX2). member(A-ACount, TrueNs), member(B-BCount, TrueNs), A < B,
Dist is (B - A) * ACount * BCount.
true_coord(Axis, Map, X, TrueX) :- true_coords(x, Map, TrueXs) :-
findall(E, (call(galaxies, Axis, Map, E-0), E < X), EmptyRows), NextX = [Row, LastX-_, X-Count]>>(
length(EmptyRows, ToAdd), sum_list(Row, Count),
TrueX is X + ToAdd. (Count = 0 -> X is LastX + 2; X is LastX + 1)),
scanl(NextX, Map, -1-0, Xs), Xs = [_ | TrueXs].
% galaxies(Axis, Map, X-Count): There are Count galaxies at coordinate X on Axis true_coords(y, [FirstRow | Map], TrueYs) :-
galaxies(x, Map, X-Count) :- nth0(X, Map, Row), foldl(plus, Row, 0, Count). foldl(add_vectors, Map, FirstRow, CountY),
galaxies(y, Map, Y-Count) :- NextY = [Count, LastY-_, Y-Count]>>(
foldl({Y}/[Row, V0, V]>>(nth0(Y, Row, C), V is V0 + C), Map, 0, Count). Count = 0 -> Y is LastY + 2; Y is LastY + 1),
scanl(NextY, CountY, -1-0, Ys), Ys = [_ | TrueYs].
add_vectors([], [], []).
add_vectors([A | V1], [B | V2], [C | V]) :- C is A + B, add_vectors(V1, V2, V).
input(FileName, Map) :- phrase_from_file(lines(Map), FileName). input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
lines([]) --> eos, !. lines([]) --> eos, !.

View File

@ -1,34 +1,31 @@
:- use_module(library(pio)). :- use_module(library(pio)).
:- initialization(main, main). :- initialization(main, main).
:- table galaxies/3.
main([FileName | _]) :- main([FileName | _]) :-
input(FileName, Map), input(FileName, Map),
findall(Dist, findall(Dist, distance(_, Map, _, Dist), Dists),
( galaxies(Axis, Map, A-ACount), ACount =\= 0,
galaxies(Axis, Map, B-BCount), BCount =\= 0,
A < B,
distance(Axis, Map, A, B, DistPair),
Dist is DistPair * ACount * BCount),
Dists),
% Sum of manhattan distances is sum of distances on all axes.
sum_list(Dists, Sum), sum_list(Dists, Sum),
writef('Answer=%t\n', [Sum]). writef('Answer=%t\n', [Sum]).
distance(Axis, Map, X1, X2, Dist) :- distance(Axis, Map, A-B, Dist) :-
true_coord(Axis, Map, X1, TrueX1), true_coord(Axis, Map, X2, TrueX2), true_coords(Axis, Map, TrueNs),
Dist is abs(TrueX1 - TrueX2). member(A-ACount, TrueNs), member(B-BCount, TrueNs), A < B,
Dist is (B - A) * ACount * BCount.
true_coord(Axis, Map, X, TrueX) :- true_coords(x, Map, TrueXs) :-
findall(E, (call(galaxies, Axis, Map, E-0), E < X), EmptyRows), NextX = [Row, LastX-_, X-Count]>>(
length(EmptyRows, ToAdd), sum_list(Row, Count),
TrueX is X + ToAdd*999999. (Count = 0 -> X is LastX + 1000000; X is LastX + 1)),
scanl(NextX, Map, -1-0, Xs), Xs = [_ | TrueXs].
% galaxies(Axis, Map, X-Count): There are Count galaxies at coordinate X on Axis true_coords(y, [FirstRow | Map], TrueYs) :-
galaxies(x, Map, X-Count) :- nth0(X, Map, Row), foldl(plus, Row, 0, Count). foldl(add_vectors, Map, FirstRow, CountY),
galaxies(y, Map, Y-Count) :- NextY = [Count, LastY-_, Y-Count]>>(
foldl({Y}/[Row, V0, V]>>(nth0(Y, Row, C), V is V0 + C), Map, 0, Count). Count = 0 -> Y is LastY + 1000000; Y is LastY + 1),
scanl(NextY, CountY, -1-0, Ys), Ys = [_ | TrueYs].
add_vectors([], [], []).
add_vectors([A | V1], [B | V2], [C | V]) :- C is A + B, add_vectors(V1, V2, V).
input(FileName, Map) :- phrase_from_file(lines(Map), FileName). input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
lines([]) --> eos, !. lines([]) --> eos, !.