improve day 9.2 prolog

This commit is contained in:
chayleaf 2021-12-09 14:18:09 +07:00
parent 34f2b8eff0
commit ca546037f1

View file

@ -28,91 +28,42 @@ basins(A, [P|Ps], [B|Bs]) :-
length(Bas, B),
basins(A, Ps, Bs).
low_point([[A,B|_]|[[C|_]|_]], 0, 0) :-
A < B, A < C.
fetch_value(A, [X, Y], V) :-
nth0(Y, A, R),
nth0(X, R, V).
low_point([A|As], X, 0) :-
X > 0,
nth0(X, A, R0),
X0 is X - 1,
X1 is X + 1,
nth0(X0, A, R1),
R0 < R1,
(
(length(A, X1), length([A|As], 1));
(length([A|As], 1), nth0(X1, A, R2), R0 < R2);
(length(A, X1), nth0(1, [A|As], E), nth0(X, E, E0), R0 < E0);
(nth0(X1, A, R2), R0 < R2, nth0(1, [A|As], E), nth0(X, E, E0), R0 < E0)
).
low_point(A, 0, Y) :-
Y > 0,
nth0(Y, A, [R0,R1|_]),
R0 < R1,
Y0 is Y - 1,
Y1 is Y + 1,
nth0(Y0, A, [T0|_]),
R0 < T0,
(
length(A, Y1);
(nth0(Y1, A, [E0|_]), R0 < E0)
).
fetch_values(_, [], []).
fetch_values(A, [P|Ps], [V|Vs]) :-
fetch_value(A, P, V),
fetch_values(A, Ps, Vs).
low_point(A, X, Y) :-
Y > 0,
X > 0,
nth0(Y, A, R),
nth0(X, R, R0),
X0 is X - 1,
Y0 is Y - 1,
X1 is X + 1,
Y1 is Y + 1,
nth0(X0, R, R1),
R0 < R1,
nth0(Y0, A, E),
nth0(X, E, E0),
R0 < E0,
valid_paths(A, X, Y, P),
fetch_values(A, P, V),
fetch_value(A, [X, Y], V0),
min_list(V, V1),
V1 > V0.
low_points_rest([A|As], X, Y, Rs) :-
length(A, W),
(
(length(A, Y1), length(R, X1));
(length(A, Y1), nth0(X1, R, R2), R0 < R2);
(length(R, X1), nth0(Y1, A, T), nth0(X, T, T0), R0 < T0);
(nth0(X1, R, R2), R0 < R2, nth0(Y1, A, T), nth0(X, T, T0), R0 < T0)
(X > 0, X0 is X - 1, low_points([A|As], X0, Y, Rs));
(X = 0, Y > 0, X0 is W - 1, Y0 is Y - 1, low_points([A|As], X0, Y0, Rs))
).
low_points([A|As], Ret) :-
length([A|As], Y),
length(A, X),
X0 is X - 1,
Y0 is Y - 1,
low_points([A|As], X0, Y0, Ret).
low_points(A, 0, 0, [[0, 0]]) :-
low_point(A, 0, 0).
low_points(A, 0, 0, []) :-
\+ low_point(A, 0, 0).
low_points(A, 0, 0, [[0, 0]]) :- low_point(A, 0, 0).
low_points(A, 0, 0, []) :- \+ low_point(A, 0, 0).
low_points(A, X, Y, [[X, Y]|Rs]) :-
X > 0,
low_point(A, X, Y),
X0 is X - 1,
low_points(A, X0, Y, Rs).
low_points_rest(A, X, Y, Rs).
low_points(A, X, Y, Rs) :-
X > 0,
\+ low_point(A, X, Y),
X0 is X - 1,
low_points(A, X0, Y, Rs).
low_points([A|As], 0, Y, [[0, Y]|Rs]) :-
Y > 0,
length(A, Len),
low_point([A|As], 0, Y),
Y0 is Y - 1,
X0 is Len - 1,
low_points([A|As], X0, Y0, Rs).
low_points([A|As], 0, Y, Rs) :-
Y > 0,
length(A, Len),
\+ low_point([A|As], 0, Y),
Y0 is Y - 1,
X0 is Len - 1,
low_points([A|As], X0, Y0, Rs).
low_points_rest(A, X, Y, Rs).
low_points([A|As], Ret) :-
length([A|As], H),
length(A, W),
X0 is W - 1,
Y0 is H - 1,
low_points([A|As], X0, Y0, Ret).
high_neighbor(A, [X1, Y1], [X0, Y0]) :-
nth0(Y1, A, R1),