aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-07 12:29:17 +0100
committerGitHub <noreply@github.com>2024-07-07 12:29:17 +0100
commit471df33103342abdf076c6a2c8a923f90ed37773 (patch)
tree56c3daf3e4520de79fdd23069755db2d68d70924
parent1b5344203101d280cf796590cf49b879313a104e (diff)
parent740d8c5b91ceceeee5ffc2410378fd1c5e1427a2 (diff)
downloadperlweeklychallenge-club-471df33103342abdf076c6a2c8a923f90ed37773.tar.gz
perlweeklychallenge-club-471df33103342abdf076c6a2c8a923f90ed37773.tar.bz2
perlweeklychallenge-club-471df33103342abdf076c6a2c8a923f90ed37773.zip
Merge pull request #10372 from 2colours/branch-for-prolog-275-276
Add remaining Prolog solutions for week 275 and 276
-rw-r--r--challenge-275/2colours/prolog/ch-1.p8
-rw-r--r--challenge-276/2colours/prolog/ch-1.p2
-rw-r--r--challenge-276/2colours/prolog/ch-2.p8
3 files changed, 18 insertions, 0 deletions
diff --git a/challenge-275/2colours/prolog/ch-1.p b/challenge-275/2colours/prolog/ch-1.p
new file mode 100644
index 0000000000..788051a5c1
--- /dev/null
+++ b/challenge-275/2colours/prolog/ch-1.p
@@ -0,0 +1,8 @@
+match_collector(Match, [Match.get(0) | Remaining], Remaining).
+
+split_word(String, Words) :- re_foldl(match_collector, "\\S+", String, Words, [], []).
+
+create_matcher(Keys, Matcher) :- format(string(Regex), "^[^~s]+$", [Keys]), Matcher = [Word]>>re_match(Regex/i, Word).
+% create_matcher(Keys, Matcher) :- format(string(Regex), "^[^~s]+$", [Keys]), Matcher = re_match(Regex/i). % without the explicit lambda
+
+task1(Sentence, Keys, Nr_Typable_Words) :- split_word(Sentence, Words), create_matcher(Keys, Matcher), include(Matcher, Words, Allowed_Words), length(Allowed_Words, Nr_Typable_Words).
diff --git a/challenge-276/2colours/prolog/ch-1.p b/challenge-276/2colours/prolog/ch-1.p
new file mode 100644
index 0000000000..b952b23d49
--- /dev/null
+++ b/challenge-276/2colours/prolog/ch-1.p
@@ -0,0 +1,2 @@
+:- use_module(library(clpfd)).
+task1(Hours, Nr_Complete_Day_Pairs) :- findall([V, W], (nth0(I, Hours, V), J #> I, nth0(J, Hours, W), (V - W) mod 24 =:= 0), Res), length(Res, Nr_Complete_Day_Pairs).
diff --git a/challenge-276/2colours/prolog/ch-2.p b/challenge-276/2colours/prolog/ch-2.p
new file mode 100644
index 0000000000..259640b286
--- /dev/null
+++ b/challenge-276/2colours/prolog/ch-2.p
@@ -0,0 +1,8 @@
+aggregate_bag(Value-Amount, [], [Value-Amount]).
+aggregate_bag(Value-Amount, [Value-Old_Amount|Rest], [Value-New_Amount|Rest]) :- !, New_Amount is Old_Amount + Amount.
+aggregate_bag(Value-Amount, [Other_Value-Amount2|Rest], [Value-Amount, Other_Value-Amount2|Rest]).
+
+% task2(Ints, Most_Frequent_Count) :- clumped(Ints, Ints_RLE), msort(Ints_RLE, Sorted_RLE), foldl(aggregate_bag, Sorted_RLE, [], Aggregated_RLE), pairs_values(Aggregated_RLE, Counts), max_list(Counts, Max_Count), include(=(Max_Count), Counts, All_Maxes), sum_list(All_Maxes, Most_Frequent_Count).
+
+task2(Ints, Most_Frequent_Count) :- msort(Ints, Sorted_Ints), clumped(Sorted_Ints, Ints_Counted), pairs_values(Ints_Counted, Counts), max_list(Counts, Max_Count), include(=(Max_Count), Counts, All_Maxes), sum_list(All_Maxes, Most_Frequent_Count).
+