aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author2colours <polgar.marton@windowslive.com>2024-07-12 23:49:08 +0200
committer2colours <polgar.marton@windowslive.com>2024-07-12 23:49:08 +0200
commitff76d93cca121fe36b84416e3a8ed28032bfebb6 (patch)
treeda490a48eaee2e4e942783ebf66f5e97eec6472d
parentbf4eb71a4e5ef70445e3319aba7e9666235a1ecf (diff)
downloadperlweeklychallenge-club-ff76d93cca121fe36b84416e3a8ed28032bfebb6.tar.gz
perlweeklychallenge-club-ff76d93cca121fe36b84416e3a8ed28032bfebb6.tar.bz2
perlweeklychallenge-club-ff76d93cca121fe36b84416e3a8ed28032bfebb6.zip
Prolog solutions by 2colours
-rw-r--r--challenge-277/2colours/prolog/ch-1.p17
-rw-r--r--challenge-277/2colours/prolog/ch-2.p14
2 files changed, 31 insertions, 0 deletions
diff --git a/challenge-277/2colours/prolog/ch-1.p b/challenge-277/2colours/prolog/ch-1.p
new file mode 100644
index 0000000000..e02e5b8728
--- /dev/null
+++ b/challenge-277/2colours/prolog/ch-1.p
@@ -0,0 +1,17 @@
+build_uniq_assoc(Value, Assoc_Before, Assoc_After) :-
+ put_assoc(Value, Assoc_Before, Valid_After, Assoc_After),
+ (get_assoc(Value, Assoc_Before, _Valid_Before) ->
+ Valid_After = false ;
+ Valid_After = true).
+
+task1(Words1, Words2, Nr_Common_Once) :-
+ empty_assoc(E),
+ maplist([Words, Result_Assoc]>>foldl(build_uniq_assoc, Words, E, Result_Assoc), [Words1, Words2], [Unique_Assoc1, Unique_Assoc2]),
+ findall([Word], (
+ gen_assoc(Word, Unique_Assoc1, Valid1),
+ get_assoc(Word, Unique_Assoc2, Valid2),
+ Valid1 == true,
+ Valid2 == true
+ ),
+ Good_Words),
+ length(Good_Words, Nr_Common_Once).
diff --git a/challenge-277/2colours/prolog/ch-2.p b/challenge-277/2colours/prolog/ch-2.p
new file mode 100644
index 0000000000..8c8a36b220
--- /dev/null
+++ b/challenge-277/2colours/prolog/ch-2.p
@@ -0,0 +1,14 @@
+:- use_module(library(clpfd)).
+
+find_value2(List, Index, Value, Value2) :-
+ Index2 #> Index,
+ nth0(Index2, List, Value2),
+ (Value2 - Value >= Value, !, fail ; true).
+
+task2(Ints, Nr_Strong_Pairs) :-
+ sort(Ints, Ints_Sorted),
+ findall([Value, Value2],
+ (nth0(Index, Ints_Sorted, Value),
+ find_value2(Ints_Sorted, Index, Value, Value2)),
+ Solutions),
+ length(Solutions, Nr_Strong_Pairs).