cleanup d11 code
This commit is contained in:
parent
963c06b3ba
commit
a437ae1e1c
@ -1,28 +1,31 @@
|
||||
% usase: swipl day11.pl input.txt 1000000
|
||||
:- use_module(library(pio)).
|
||||
:- initialization(main, main).
|
||||
|
||||
main([FileName | _]) :-
|
||||
main([FileName, ExpandFactorArg | _]) :-
|
||||
input(FileName, Map),
|
||||
findall(Dist, distance(_, Map, _, Dist), Dists),
|
||||
atom_number(ExpandFactorArg, ExpandFactor),
|
||||
findall(Dist, distance(_, Map, _, ExpandFactor, Dist), Dists),
|
||||
sum_list(Dists, Sum),
|
||||
writef('Answer=%t\n', [Sum]).
|
||||
|
||||
% 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),
|
||||
distance(Axis, Map, A-B, ExpandFactor, Dist) :-
|
||||
true_coords(Axis, Map, ExpandFactor, TrueNs),
|
||||
member(A-ACount, TrueNs), member(B-BCount, TrueNs), A < B,
|
||||
Dist is (B - A) * ACount * BCount.
|
||||
|
||||
% TrueXs is the list of all true coordinates along Axis in Map
|
||||
true_coords(x, Map, TrueXs) :-
|
||||
NextX = [Row, LastX-_, X-Count]>>(
|
||||
% TrueXs is the list of all coordinates after expanion along Axis in Map
|
||||
true_coords(x, Map, ExpandFactor, TrueXs) :-
|
||||
NextX = {ExpandFactor}/[Row, LastX-_, X-Count]>>(
|
||||
sum_list(Row, Count),
|
||||
(Count = 0 -> X is LastX + 2; X is LastX + 1)),
|
||||
(Count = 0 -> X is LastX + ExpandFactor; X is LastX + 1)),
|
||||
scanl(NextX, Map, -1-0, Xs), Xs = [_ | TrueXs].
|
||||
true_coords(y, [FirstRow | Map], TrueYs) :-
|
||||
|
||||
true_coords(y, [FirstRow | Map], ExpandFactor, TrueYs) :-
|
||||
foldl(add_vectors, Map, FirstRow, CountY),
|
||||
NextY = [Count, LastY-_, Y-Count]>>(
|
||||
Count = 0 -> Y is LastY + 2; Y is LastY + 1),
|
||||
NextY = {ExpandFactor}/[Count, LastY-_, Y-Count]>>(
|
||||
Count = 0 -> Y is LastY + ExpandFactor; Y is LastY + 1),
|
||||
scanl(NextY, CountY, -1-0, Ys), Ys = [_ | TrueYs].
|
||||
|
||||
add_vectors([], [], []).
|
||||
@ -30,8 +33,9 @@ add_vectors([A | V1], [B | V2], [C | V]) :- C is A + B, add_vectors(V1, V2, V).
|
||||
|
||||
% Read file into 2D array Map. 1 corresponding to a galaxy and 0 otherwise.
|
||||
input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
|
||||
lines([]) --> eos, !.
|
||||
lines([]) --> eos.
|
||||
lines([Line|Lines]) --> line(Line), lines(Lines).
|
||||
line([]) --> ( "\n" ; eos ), !.
|
||||
line([L|Ls]) --> ([C], {C =:= 35 -> L = 1; L = 0}), line(Ls).
|
||||
line([]) --> "\n" ; eos.
|
||||
line([1|Ls]) --> "#", line(Ls).
|
||||
line([0|Ls]) --> ".", line(Ls).
|
||||
eos([], []).
|
37
11/part2.pl
37
11/part2.pl
@ -1,37 +0,0 @@
|
||||
:- use_module(library(pio)).
|
||||
:- initialization(main, main).
|
||||
|
||||
main([FileName | _]) :-
|
||||
input(FileName, Map),
|
||||
findall(Dist, distance(_, Map, _, Dist), Dists),
|
||||
sum_list(Dists, Sum),
|
||||
writef('Answer=%t\n', [Sum]).
|
||||
|
||||
% 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.
|
||||
|
||||
% 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 + 1000000; 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 + 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).
|
||||
|
||||
% Read file into 2D array Map. 1 corresponding to a galaxy and 0 otherwise.
|
||||
input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
|
||||
lines([]) --> eos, !.
|
||||
lines([Line|Lines]) --> line(Line), lines(Lines).
|
||||
line([]) --> ( "\n" ; eos ), !.
|
||||
line([L|Ls]) --> ([C], {C =:= 35 -> L = 1; L = 0}), line(Ls).
|
||||
eos([], []).
|
Loading…
Reference in New Issue
Block a user