:- consult(library(clpfd)).
:- consult(library(apply)).

% Aufgabe:
% https://cloud.phil.hhu.de/index.php/s/6SDO1CF9AtMaEru

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loesung:

getbounds(task(Start,_,End,_,_),Start,End).

tasks([      %% Start, Duration, End, Consumption, Identifier
         task(_, 1, _, 1, "Rainbow Dash erschrecken"),
         task(_, 2, _, 1, "Twilight als Kanone verwenden"),
         task(_, 10, EndAppleTrip, 1, "Familien-Ausflug mit den Apples"),
         task(RockSoup, 1, _, 1, "Steinsuppe schlürfen"),
         task(_, 1, _, 1, "In Fluttershys Haaren verstecken"),
         task(_, 8, EndVisitYaks, 1, "Die Yaks in Yakyakistan besuchen"),
         task(YakSnow, 1, _, 1, "Im yakyakistanischen Schnee wälzen"),
         task(_, 4, EndPssdBuy, 1, "Geschenk für Maud kaufen"),
         task(PssdGive, 1, EndPssdGive, 1, "Geschenk an Maud überreichen"),
         task(CiderQueue, 2, EndCiderQueue, 1, "Für Cider anstehen"),
         task(CiderDrink, 4, _, 1, "Mit Maud Cider trinken"),
         task(_, 1, _, 1, "Schokoregen trinken"),
         task(_, 5, _, 1, "Den Keksdieb finden"),
         task(_, 2, _, 1, "Kuchen backen mit Starlight"),
         task(_, 2, _, LakePinkies, "See stürmen")]) :-
    RockSoup #>= EndAppleTrip,
    PssdGive #>= EndPssdBuy,
    CiderQueue #>= EndPssdGive,
    CiderDrink #>= EndCiderQueue,
    YakSnow #>= EndVisitYaks,
    LakePinkies #>= 3.

sched_day :-
    tasks(Tasks),
    maplist(getbounds, Tasks, Starts, Ends),
    Starts ins 7..22,
    Ends ins 8..23,
    cumulative(Tasks,[limit(4)]),
    label(Starts),
    maplist(writetask,Tasks).

writetask(task(Start,_,End,_,Text)) :-
    writetime(Start), write("-"), writetime(End),
    write("  "), write(Text), nl.
writetime(T) :- T < 10, write(" "), write(T), write(":00"),!.
writetime(T) :- write(T), write(":00").
