better d16p2

This commit is contained in:
Dory 2023-12-15 23:47:00 -08:00
parent 793bb420ea
commit 2202ebf4a8

View File

@ -5,12 +5,15 @@ main([FileName|_]) :-
input(FileName, Map),
length(Map, Height), MaxX is Height - 1,
Map = [Row|_], length(Row, Width), MaxY is Width - 1,
findall(N, (between(0, MaxX, X), propagate(Map, [X-0-3], End), count(End, N)), N1), nl,
findall(N, (between(0, MaxX, X), propagate(Map, [X-MaxY-1], End), count(End, N)), N2), nl,
findall(N, (between(0, MaxY, Y), propagate(Map, [0-Y-0], End), count(End, N)), N3), nl,
findall(N, (between(0, MaxY, Y), propagate(Map, [MaxX-Y-2], End), count(End, N)), N4), nl,
append([N1, N2, N3, N4], Ns), max_list(Ns, Answer),
write(Answer), nl.
findall([X-0-3], between(0, MaxX, X), S1),
findall([X-MaxY-1], between(0, MaxX, X), S2),
findall([0-Y-0], between(0, MaxY, Y), S3),
findall([MaxX-Y-2], between(0, MaxY, Y), S4),
append([S1, S2, S3, S4], Starts),
concurrent_maplist(
{Map}/[S, N]>>(propagate(Map, S, End), count(End, N)), Starts, Ns),
max_list(Ns, Answer),
nl, write(Answer), nl.
count(Map, X) :-
findall(1, (nth0(_, Map, Row), nth0(_, Row, _-D), \+ D = [0,0,0,0]), Ls),