diff options
| -rw-r--r-- | challenge-303/2colours/prolog/ch-1.p | 14 | ||||
| -rw-r--r-- | challenge-303/2colours/prolog/ch-2.p | 36 |
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-303/2colours/prolog/ch-1.p b/challenge-303/2colours/prolog/ch-1.p new file mode 100644 index 0000000000..60404000b4 --- /dev/null +++ b/challenge-303/2colours/prolog/ch-1.p @@ -0,0 +1,14 @@ +variation(Set, Size, Variation, Set_Rest) :- + foldl([Choice, Remaining_Before-Set_Before, Remaining_After-Set_After]>>( + select(Choice, Set_Before, Set_After), + plus(Remaining_After, 1, Remaining_Before) + ), Variation, Size-Set, 0-Set_Rest). + +task1(Ints, Unique_Solutions) :- + findall(Number, ( + variation(Ints, 3, Variation, _Rest), + [First_Digit|_] = Variation, First_Digit > 0, + foldl([Digit, Num_Before, Num_After]>>(Num_After is Num_Before * 10 + Digit), Variation, 0, Number), + Number mod 2 =:= 0 + ), Numbers), + list_to_ord_set(Numbers, Unique_Solutions). diff --git a/challenge-303/2colours/prolog/ch-2.p b/challenge-303/2colours/prolog/ch-2.p new file mode 100644 index 0000000000..d95e3883ba --- /dev/null +++ b/challenge-303/2colours/prolog/ch-2.p @@ -0,0 +1,36 @@ +:- use_module(library(dcg/high_order)). + +ascension([Value1-Count1, Value2-Count2 | Rest]) --> [Value1-Count1], { plus(Value1, 1, Value2) }, ascension([Value2-Count2|Rest]). +ascension([Head]) --> [Head]. + +max_score([], 0). +max_score([Value], Value). +max_score([V1, V2], Score) :- Score is max(V1, V2). +max_score(Values, Score) :- + length(Values, Orig_Length), + Orig_Length >= 3, + Half_Without_Lower_Length is Orig_Length div 2 - 1, + Half_Length is Orig_Length div 2, + length(First_Part_Without_Lower, Half_Without_Lower_Length), + append(First_Part_Without_Lower, [_Lower, Pivot, Upper|Second_Part_Without_Upper], Values), + length(First_Part, Half_Length), + append(First_Part, _Rest, Values), + max_score(First_Part_Without_Lower, FPS), + max_score(Second_Part_Without_Upper, SPS), + Score_With_Pivot is FPS + SPS + Pivot, + max_score(First_Part, FPLS), + max_score([Upper|Second_Part_Without_Upper], USPS), + Score_Without_Pivot is FPLS + USPS, + Score is max(Score_With_Pivot, Score_Without_Pivot). + +% this is the solution for the case where we absolutely *must* clear the whole array, even at the cost of taking =< 0 values +% otherwise, we could just erase those values from the input before ever doing anything because all they ever could do is decrease the score we can get +task2(Ints, Max_Score) :- + msort(Ints, Ints_Sorted), + clumped(Ints_Sorted, Ints_Grouped), + once(phrase(sequence(ascension, Ascensions), Ints_Grouped)), + maplist([Group, Max_Group_Score]>>( + maplist([Value-Count, Worth]>>(Worth is Value * Count), Group, Worths), + max_score(Worths, Max_Group_Score) + ), Ascensions, Max_Scores), + sum_list(Max_Scores, Max_Score). |
