2023-12-11 01:48:51 -06:00
|
|
|
:- use_module(library(pio)).
|
2023-12-11 14:33:06 -06:00
|
|
|
:- initialization(main, main).
|
2023-12-11 01:48:51 -06:00
|
|
|
|
2023-12-11 14:33:06 -06:00
|
|
|
main([FileName | _]) :-
|
|
|
|
input(FileName, Map),
|
2023-12-11 19:16:18 -06:00
|
|
|
findall(Dist, distance(_, Map, _, Dist), Dists),
|
2023-12-11 14:33:06 -06:00
|
|
|
sum_list(Dists, Sum),
|
|
|
|
writef('Answer=%t\n', [Sum]).
|
2023-12-11 01:48:51 -06:00
|
|
|
|
2023-12-11 19:16:18 -06:00
|
|
|
% Dist is distance along Axis in Map between two row/col A & B (after expansion)
|
|
|
|
distance(Axis, Map, A-B, Dist) :-
|
|
|
|
true_coords(Axis, Map, TrueNs),
|
|
|
|
member(A-ACount, TrueNs), member(B-BCount, TrueNs), A < B,
|
|
|
|
Dist is (B - A) * ACount * BCount.
|
2023-12-11 01:48:51 -06:00
|
|
|
|
2023-12-11 19:16:18 -06:00
|
|
|
% TrueXs is the list of all true coordinates along Axis in Map
|
|
|
|
true_coords(x, Map, TrueXs) :-
|
|
|
|
NextX = [Row, LastX-_, X-Count]>>(
|
|
|
|
sum_list(Row, Count),
|
|
|
|
(Count = 0 -> X is LastX + 2; X is LastX + 1)),
|
|
|
|
scanl(NextX, Map, -1-0, Xs), Xs = [_ | TrueXs].
|
|
|
|
true_coords(y, [FirstRow | Map], TrueYs) :-
|
|
|
|
foldl(add_vectors, Map, FirstRow, CountY),
|
|
|
|
NextY = [Count, LastY-_, Y-Count]>>(
|
|
|
|
Count = 0 -> Y is LastY + 2; Y is LastY + 1),
|
|
|
|
scanl(NextY, CountY, -1-0, Ys), Ys = [_ | TrueYs].
|
2023-12-11 01:48:51 -06:00
|
|
|
|
2023-12-11 19:16:18 -06:00
|
|
|
add_vectors([], [], []).
|
|
|
|
add_vectors([A | V1], [B | V2], [C | V]) :- C is A + B, add_vectors(V1, V2, V).
|
2023-12-11 01:48:51 -06:00
|
|
|
|
2023-12-11 19:16:18 -06:00
|
|
|
% Read file into 2D array Map. 1 corresponding to a galaxy and 0 otherwise.
|
2023-12-11 01:48:51 -06:00
|
|
|
input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
|
|
|
|
lines([]) --> eos, !.
|
|
|
|
lines([Line|Lines]) --> line(Line), lines(Lines).
|
|
|
|
line([]) --> ( "\n" ; eos ), !.
|
2023-12-11 14:33:06 -06:00
|
|
|
line([L|Ls]) --> ([C], {C =:= 35 -> L = 1; L = 0}), line(Ls).
|
|
|
|
eos([], []).
|