aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-01-13 10:19:18 +0000
committerGitHub <noreply@github.com>2025-01-13 10:19:18 +0000
commit577a5a4d80892fa7b04a3e3b39fc47ff9a2c66ec (patch)
tree722d06ca6f0718237dd7a5429a83aa3762019f4c
parent912ec73bfae771ed360adcd1e979c398a387d678 (diff)
parentec5bd9b8dfa0b9738319acf3f251d71b42af2781 (diff)
downloadperlweeklychallenge-club-577a5a4d80892fa7b04a3e3b39fc47ff9a2c66ec.tar.gz
perlweeklychallenge-club-577a5a4d80892fa7b04a3e3b39fc47ff9a2c66ec.tar.bz2
perlweeklychallenge-club-577a5a4d80892fa7b04a3e3b39fc47ff9a2c66ec.zip
Merge pull request #11437 from 2colours/branch-for-challenge-303
Prolog solutions for week #303
-rw-r--r--challenge-303/2colours/prolog/ch-1.p14
-rw-r--r--challenge-303/2colours/prolog/ch-2.p36
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).