Compare commits
2 Commits
330c51da3a
...
3024e45da7
Author | SHA1 | Date | |
---|---|---|---|
3024e45da7 | |||
464d12cea0 |
@ -1,4 +1,3 @@
|
|||||||
:- op(700, xfx, before).
|
|
||||||
:- table galaxy/2.
|
:- table galaxy/2.
|
||||||
:- table no_galaxy_row/2.
|
:- table no_galaxy_row/2.
|
||||||
:- table no_galaxy_col/2.
|
:- table no_galaxy_col/2.
|
||||||
@ -7,13 +6,14 @@
|
|||||||
|
|
||||||
start :-
|
start :-
|
||||||
input('input.txt', Map),
|
input('input.txt', Map),
|
||||||
findall(Dist, (galaxy(Map, A), galaxy(Map, B), A before B,
|
findall(Dist, distance(Map, _, _, Dist), Dists),
|
||||||
distance(Map, A, B, Dist)),
|
|
||||||
Dists),
|
|
||||||
sum_list(Dists, Answer),
|
sum_list(Dists, Answer),
|
||||||
writef('Answer=%w\n', [Answer]).
|
writef('Answer=%w\n', [Answer]).
|
||||||
|
|
||||||
distance(Map, X1-Y1, X2-Y2, Dist) :-
|
distance(Map, X1-Y1, X2-Y2, Dist) :-
|
||||||
|
galaxy(Map, X1-Y1),
|
||||||
|
galaxy(Map, X2-Y2),
|
||||||
|
(X1 < X2; X1 =:= X2, Y1 < Y2),
|
||||||
findall(X, no_galaxy_row(Map, X), EmptyXs),
|
findall(X, no_galaxy_row(Map, X), EmptyXs),
|
||||||
findall(Y, no_galaxy_col(Map, Y), EmptyYs),
|
findall(Y, no_galaxy_col(Map, Y), EmptyYs),
|
||||||
include(between(X1, X2), EmptyXs, ExpandedX), length(ExpandedX, RowsToAdd),
|
include(between(X1, X2), EmptyXs, ExpandedX), length(ExpandedX, RowsToAdd),
|
||||||
@ -25,7 +25,6 @@ no_galaxy_row(Map, X) :- nth0(X, Map, Row), maplist([46]>>(true), Row).
|
|||||||
no_galaxy_col(Map, Y) :- maplist({Y}/[Row]>>(nth0(Y, Row, 46)), Map).
|
no_galaxy_col(Map, Y) :- maplist({Y}/[Row]>>(nth0(Y, Row, 46)), Map).
|
||||||
|
|
||||||
galaxy(Map, X-Y) :- nth0(X, Map, Row), nth0(Y, Row, 35).
|
galaxy(Map, X-Y) :- nth0(X, Map, Row), nth0(Y, Row, 35).
|
||||||
before(X1-Y1, X2-Y2) :- X1 < X2; X1 =:= X2, Y1 < Y2.
|
|
||||||
|
|
||||||
input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
|
input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
|
||||||
lines([]) --> eos, !.
|
lines([]) --> eos, !.
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
:- op(700, xfx, before).
|
|
||||||
:- table galaxy/2.
|
:- table galaxy/2.
|
||||||
:- table no_galaxy_row/2.
|
:- table no_galaxy_row/2.
|
||||||
:- table no_galaxy_col/2.
|
:- table no_galaxy_col/2.
|
||||||
@ -7,13 +6,14 @@
|
|||||||
|
|
||||||
start :-
|
start :-
|
||||||
input('input.txt', Map),
|
input('input.txt', Map),
|
||||||
findall(Dist, (galaxy(Map, A), galaxy(Map, B), A before B,
|
findall(Dist, distance(Map, _, _, Dist), Dists),
|
||||||
distance(Map, A, B, Dist)),
|
|
||||||
Dists),
|
|
||||||
sum_list(Dists, Answer),
|
sum_list(Dists, Answer),
|
||||||
writef('Answer=%w\n', [Answer]).
|
writef('Answer=%w\n', [Answer]).
|
||||||
|
|
||||||
distance(Map, X1-Y1, X2-Y2, Dist) :-
|
distance(Map, X1-Y1, X2-Y2, Dist) :-
|
||||||
|
galaxy(Map, X1-Y1),
|
||||||
|
galaxy(Map, X2-Y2),
|
||||||
|
(X1 < X2; X1 =:= X2, Y1 < Y2),
|
||||||
findall(X, no_galaxy_row(Map, X), EmptyXs),
|
findall(X, no_galaxy_row(Map, X), EmptyXs),
|
||||||
findall(Y, no_galaxy_col(Map, Y), EmptyYs),
|
findall(Y, no_galaxy_col(Map, Y), EmptyYs),
|
||||||
include(between(X1, X2), EmptyXs, ExpandedX), length(ExpandedX, RowsToAdd),
|
include(between(X1, X2), EmptyXs, ExpandedX), length(ExpandedX, RowsToAdd),
|
||||||
@ -25,7 +25,6 @@ no_galaxy_row(Map, X) :- nth0(X, Map, Row), maplist([46]>>(true), Row).
|
|||||||
no_galaxy_col(Map, Y) :- maplist({Y}/[Row]>>(nth0(Y, Row, 46)), Map).
|
no_galaxy_col(Map, Y) :- maplist({Y}/[Row]>>(nth0(Y, Row, 46)), Map).
|
||||||
|
|
||||||
galaxy(Map, X-Y) :- nth0(X, Map, Row), nth0(Y, Row, 35).
|
galaxy(Map, X-Y) :- nth0(X, Map, Row), nth0(Y, Row, 35).
|
||||||
before(X1-Y1, X2-Y2) :- X1 < X2; X1 =:= X2, Y1 < Y2.
|
|
||||||
|
|
||||||
input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
|
input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
|
||||||
lines([]) --> eos, !.
|
lines([]) --> eos, !.
|
||||||
|
Loading…
Reference in New Issue
Block a user