I've read about this problem in the book Olympiads in Informatics. I managed to solve it in haskell, but I want recode it, but I realized it's hard to transform in prolog due to too many built in function usage (and I used many of them in declarative form). So I want to rebuild it from scratch and I tried to search for similar problems solved in prolog, but I did not succeed. Can somebody help me with linking a solution of a similar problem in prolog or expand my cut predicate. Huge thanks in advance.
The problem: "The task can be summarized in the following way: there is a line of trees, with one meter of space between each of them. Each tree has a known height, in meters, and you can cut it aiming it toward its right or left. When an "m" meter tree falls, like in a domino game it forces the falling of its m−1 close trees, and this in turn can force other tree to fall. You can decide which tree to cut, and for each of them you can choose in which direction it will fall. Provide a list as short as possible of the trees to be felled, with a positive number representing a decision to the right and a negative number representing the code for a decision to the left.
My input:
tree(1,1).
tree(2,1).
tree(3,1).
tree(4,2).
tree(5,5).
tree(6,1).
My current code:
treecut(F, DL):-
see(F),
readfromfile([_|L]),
seen,
modifytolist(L, AL),
cut(AL, DL). %how to do it?
readfromfile([Line|L]):-
read(Line),
Line \= end_of_file,
readfromfile(L),
!.
readfromfile([]).
modifytolist([], []):-!.
modifytolist([H|T], [H2|T2]):-
pattern(H, H2),
modifytolist(T,T2).
pattern(tree(P,H), (P,H)).
splitree((P,H), P, H):-!.
Output what I want
treecut('input1.pl',L).
L = [(5, -1), (6, 1)].
