From 4a2d626b8cd07a735a954f21ba1ee8a18eb4c30d Mon Sep 17 00:00:00 2001 From: Shawn Wagner Date: Thu, 9 Feb 2023 01:01:00 -0800 Subject: Challenge 203.1: Solution in SWI Prolog --- challenge-203/shawn-wagner/prolog/ch-1.p | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 challenge-203/shawn-wagner/prolog/ch-1.p 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. -- cgit From 4b7306cb22dacc86ef4c4ac216648a4e11a993b1 Mon Sep 17 00:00:00 2001 From: Shawn Wagner Date: Sat, 11 Feb 2023 03:33:20 -0800 Subject: Challenge 203.2: Solution in Racket --- challenge-203/shawn-wagner/racket/ch-2.rkt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-203/shawn-wagner/racket/ch-2.rkt 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") -- cgit