diff options
| author | Shawn Wagner <shawnw.mobile@gmail.com> | 2023-02-09 01:01:00 -0800 |
|---|---|---|
| committer | Shawn Wagner <shawnw.mobile@gmail.com> | 2023-02-09 01:01:00 -0800 |
| commit | 4a2d626b8cd07a735a954f21ba1ee8a18eb4c30d (patch) | |
| tree | 0335096969d4cac2bc7203cade52ea9b36ce1d82 | |
| parent | f92e84261b474f81c014f4982268d6e2797b66d9 (diff) | |
| download | perlweeklychallenge-club-4a2d626b8cd07a735a954f21ba1ee8a18eb4c30d.tar.gz perlweeklychallenge-club-4a2d626b8cd07a735a954f21ba1ee8a18eb4c30d.tar.bz2 perlweeklychallenge-club-4a2d626b8cd07a735a954f21ba1ee8a18eb4c30d.zip | |
Challenge 203.1: Solution in SWI Prolog
| -rwxr-xr-x | challenge-203/shawn-wagner/prolog/ch-1.p | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-203/shawn-wagner/prolog/ch-1.p b/challenge-203/shawn-wagner/prolog/ch-1.p new file mode 100755 index 0000000000..8a42d2791f --- /dev/null +++ b/challenge-203/shawn-wagner/prolog/ch-1.p @@ -0,0 +1,49 @@ +#!/usr/bin/env swipl +% -*- prolog -*- + +:-initialization(main). + +use_module(library(lists)). +use_module(library(aggregate)). + +special(List, [A, B, C, D]) :- + A < B, + B < C, + C < D, + nth1(A, List, ElemA), + nth1(B, List, ElemB), + nth1(C, List, ElemC), + nth1(D, List, ElemD), + ElemA + ElemB + ElemC =:= ElemD. + +combination(0, _, []). +combination(N, [X|T], [X|Comb]) :- + N > 0, + N1 is N - 1, + combination(N1, T, Comb). +combination(N, [_|T], Comb) :- + N > 0, + combination(N, T, Comb). + +indexes([], []). +indexes(List, Indexes) :- + length(List, Len), + numlist(1, Len, I), + combination(4, I, ICombo), + permutation(ICombo, Indexes). + +find_special(Nums) :- + indexes(Nums, Indexes), + special(Nums, Indexes). + +solution(Nums, Count) :- + aggregate_all(count, find_special(Nums), Count). + +main :- + solution([1,2,3,6], Ex1), + solution([1,1,1,3,5], Ex2), + solution([3,3,6,4,5], Ex3), + format('Example 1: ~w~n', [Ex1]), + format('Example 2: ~w~n', [Ex2]), + format('Example 3: ~w~n', [Ex3]), + halt. |
