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