diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-13 05:02:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-13 05:02:34 +0000 |
| commit | 2aa5070481d05a0d5eb765e74468b29bb43db76d (patch) | |
| tree | af1355448bfd71e2fcd9605551f20befe7d52696 | |
| parent | a606e4c6c05aacb5bce51fead167c9f08e73427c (diff) | |
| parent | 4b7306cb22dacc86ef4c4ac216648a4e11a993b1 (diff) | |
| download | perlweeklychallenge-club-2aa5070481d05a0d5eb765e74468b29bb43db76d.tar.gz perlweeklychallenge-club-2aa5070481d05a0d5eb765e74468b29bb43db76d.tar.bz2 perlweeklychallenge-club-2aa5070481d05a0d5eb765e74468b29bb43db76d.zip | |
Merge pull request #7544 from shawnw/challenge-203
Challenge 203.1: Solution in SWI Prolog
| -rwxr-xr-x | challenge-203/shawn-wagner/prolog/ch-1.p | 49 | ||||
| -rw-r--r-- | challenge-203/shawn-wagner/racket/ch-2.rkt | 19 |
2 files changed, 68 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. diff --git a/challenge-203/shawn-wagner/racket/ch-2.rkt b/challenge-203/shawn-wagner/racket/ch-2.rkt new file mode 100644 index 0000000000..23f245fde7 --- /dev/null +++ b/challenge-203/shawn-wagner/racket/ch-2.rkt @@ -0,0 +1,19 @@ +#lang racket/base + +(require racket/file racket/list) + +(define (copy-directory-tree source target) + (let* ([source (string->path source)] + [target (string->path target)] + [source-component-length (length (explode-path source))] + [targets + (fold-files + (lambda (path type paths) + (if (eq? type 'dir) + (cons (apply build-path target (drop (explode-path path) source-component-length)) paths) + paths)) + '() + source)]) + (for-each make-directory* targets))) + +(copy-directory-tree "a/b/c" "x/y") |
