aoc23/09/part1.pl
2023-12-08 23:10:03 -08:00

26 lines
698 B
Prolog

% :- use_module(library(readln)).
% :- initialization(start, main).
start :-
input(Input),
findall(P, (member(L, Input), predict(L, P)), Ps),
sum_list(Ps, Answer),
writef('Answer=%t\n', [Answer]).
predict(L, 0) :- maplist(=:=(0), L), !.
predict(L, X) :-
foldl([Li1, Li2, C, D]>>(Li2 is Li1 - C, D = Li1), L, [_ | NewL], 0, _),
predict(NewL, SubX),
last(L, LastL),
X is SubX + LastL.
% this does not work very lame readln cannot handle negative numbers
% input(Lines) :-
% readln(LineTmp),
% ( (last(LineTmp, end_of_file); LineTmp = [])
% -> Lines = []
% ; Line = LineTmp,
% input(CdrLine),
% Lines = [Line | CdrLine]
% ).