diff options
| author | Abigail <abigail@abigail.be> | 2021-06-30 00:44:09 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-06-30 00:44:09 +0200 |
| commit | 20c14072c2b9a491fd414d275b6c64c6fec8e62f (patch) | |
| tree | 8de976c75da6f68a0c9ee91809dcc3a340358fdc | |
| parent | a6180cd9e375d5f36d77f7a2aac2650d5c642058 (diff) | |
| parent | 551f8dc93afa46a598644f62e74e67cdae6f0c28 (diff) | |
| download | perlweeklychallenge-club-20c14072c2b9a491fd414d275b6c64c6fec8e62f.tar.gz perlweeklychallenge-club-20c14072c2b9a491fd414d275b6c64c6fec8e62f.tar.bz2 perlweeklychallenge-club-20c14072c2b9a491fd414d275b6c64c6fec8e62f.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
89 files changed, 3824 insertions, 2271 deletions
diff --git a/challenge-012/paulo-custodio/c/ch-1.c b/challenge-012/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..967109929d --- /dev/null +++ b/challenge-012/paulo-custodio/c/ch-1.c @@ -0,0 +1,51 @@ +/* +Challenge 012 + +Challenge #1 +The numbers formed by adding one to the products of the smallest primes are +called the Euclid Numbers (see wiki). Write a script that finds the smallest +Euclid Number that is not prime. This challenge was proposed by +Laurent Rosenfeld. +*/ + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +bool is_prime(int n) { + if (n <= 1) + return false; + if (n <= 3) + return true; + if ((n % 2) == 0 || (n % 3) == 0) + return false; + for (int i = 5; i * i <= n; i += 6) + if ((n % i) == 0 || (n % (i + 2)) == 0) + return false; + return true; +} + +int next_prime(int n) { + if (n <= 1) + return 2; + do { + n++; + } while (!is_prime(n)); + return n; +} + +int next_euclid(void) { + static int prime = 1; + static int prime_prod = 1; + + prime = next_prime(prime); + prime_prod *= prime; + return prime_prod + 1; +} + +int main(void) { + int euclid; + while (is_prime(euclid = next_euclid())) + ; + printf("%d\n", euclid); +} diff --git a/challenge-012/paulo-custodio/cpp/ch-1.cpp b/challenge-012/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..b18ecaaa19 --- /dev/null +++ b/challenge-012/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,50 @@ +/* +Challenge 012 + +Challenge #1 +The numbers formed by adding one to the products of the smallest primes are +called the Euclid Numbers (see wiki). Write a script that finds the smallest +Euclid Number that is not prime. This challenge was proposed by +Laurent Rosenfeld. +*/ + +#include <iostream> +using namespace std; + +bool is_prime(int n) { + if (n <= 1) + return false; + if (n <= 3) + return true; + if ((n % 2) == 0 || (n % 3) == 0) + return false; + for (int i = 5; i * i <= n; i += 6) + if ((n % i) == 0 || (n % (i + 2)) == 0) + return false; + return true; +} + +int next_prime(int n) { + if (n <= 1) + return 2; + do { + n++; + } while (!is_prime(n)); + return n; +} + +int next_euclid(void) { + static int prime = 1; + static int prime_prod = 1; + + prime = next_prime(prime); + prime_prod *= prime; + return prime_prod + 1; +} + +int main(void) { + int euclid; + while (is_prime(euclid = next_euclid())) + ; + cout << euclid << endl; +} diff --git a/challenge-012/paulo-custodio/t/test-1.yaml b/challenge-012/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..adaf9beab6 --- /dev/null +++ b/challenge-012/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 30031 diff --git a/challenge-012/paulo-custodio/t/test-2.yaml b/challenge-012/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..975c4a1c90 --- /dev/null +++ b/challenge-012/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: / /a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e + input: + output: /a/b +- setup: + cleanup: + args: ": :a:b:c:d :a:b:cd :a:b:cc :a:b:c:d:e" + input: + output: ":a:b" +- setup: + cleanup: + args: / /b/c/d /a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e + input: + output: diff --git a/challenge-012/paulo-custodio/test.pl b/challenge-012/paulo-custodio/test.pl index db440a3a8f..ba6c37260b 100644 --- a/challenge-012/paulo-custodio/test.pl +++ b/challenge-012/paulo-custodio/test.pl @@ -1,37 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; - -is capture("perl perl/ch-1.pl"), "30031\n"; - -is capture("perl perl/ch-2.pl / ". - " /a/b/c/d ". - " /a/b/cd ". - " /a/b/cc ". - " /a/b/c/d/e "), "/a/b\n"; - -is capture("perl perl/ch-2.pl : ". - " :a:b:c:d ". - " :a:b:cd ". - " :a:b:cc ". - " :a:b:c:d:e "), ":a:b\n"; - -is capture("perl perl/ch-2.pl / ". - " /b/c/d ". - " /a/b/c/d ". - " /a/b/cd ". - " /a/b/cc ". - " /a/b/c/d/e "), "\n"; - -done_testing; - - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \r\t]*\n/\n/g; - return $out; -} +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-115/simon-proctor/raku/ch-2-2.raku b/challenge-115/simon-proctor/raku/ch-2-2.raku new file mode 100644 index 0000000000..81ba11d805 --- /dev/null +++ b/challenge-115/simon-proctor/raku/ch-2-2.raku @@ -0,0 +1,71 @@ +#!/usr/bin/env raku + +#|( Given a triangle of height $N return the possible routes from the top + To the bottom right +) +multi sub MAIN(UInt $N) { + say traverse-triangle($N).join(", "); +} + +multi sub MAIN("test") is hidden-from-USAGE { + use Test; + ok traverse-triangle(1) (==) <R LH>; + ok traverse-triangle(2) (==) <RR LHR LHLH LLHH RLH LRH>; +} + +sub traverse-triangle( $size ) { + my %seen; + my @routes = ["R" x $size]; + + while (@routes) { + my @promises; + while (@routes && @promises.elems < 1000) { + my $route = shift @routes; + next if %seen{$route}; + %seen{$route} = True; + given $route { + when /R/ { + @promises.push( start mod-right( $route ) ); + proceed; + } + when /HL/ { + @promises.push( start mod-hl( $route ) ); + } + } + } + + await @promises; + + for (@promises) -> $p { + for ( $p.result ) { + @routes.push( $_ ) if ! %seen{$_} && ($_ !(elem) @routes); + } + } + + #note @routes.elems ~ ":" ~ @promises.elems ~ ":" ~ %seen.keys.elems; + + } + + + return %seen.keys; +} + +sub mod-right( Str $row ) { + my @out; + for (0..^$row.codes) -> $i { + if ( $row.substr($i,1) eq "R" ) { + @out.push( $row.substr(0,$i) ~ "LH" ~ $row.substr($i+1) ); + } + } + return @out; +} + +sub mod-hl( Str $row ) { + my @out; + for (0..^$row.codes-1) -> $i { + if ( $row.substr($i,2) eq "HL" ) { + @out.push( $row.substr(0,$i) ~ "R" ~ $row.substr($i+2) ); + } + } + return @out; +}
\ No newline at end of file diff --git a/challenge-118/james-smith/README.md b/challenge-118/james-smith/README.md index 27cf024f18..ad9be11770 100644 --- a/challenge-118/james-smith/README.md +++ b/challenge-118/james-smith/README.md @@ -458,3 +458,152 @@ The time is now down to approximately 10 seconds. [46, 53], ] ``` + +## Task 2 - version 2 - using pre-computed distances. + +Reading CY's description using pre-computed distances I wanted to +investigate how that worked. + +### Technique + +For any 2 squares we can compute the shortest distance in "steps" +between them. + +Then we try all permutations of the order of the treasures (squares) +to compute the optimal route. In theory we can pre-cache this +information so that we don't have to calculate it every time we +execute the script (as we could have with the transition matrix above). + +So we now have the following steps: + + * Compute the transition matrix + * Compute the distance matrix - and store an example shortest route + between each pair of squares. + * Permute the points to compute a shortest route. + +### Stage 1 + +See above for computing the transition matrix + +### Stage 2 + +Computing the distance matrix. + +For each square (using our `0`..`63` notation) we generate +an array of distances and routes. To do this we start by +setting `$dist[$start][$start]` to `0` and +`$route[$start][$start]` to `''`. +We then repeatedly scan the board looking for increasing values +of distance `0`, `1`, `2` etc, and finding squares we could jump +to (that we already haven't) and set distance and routes for them. +In this case I think the code is easier to understand than the +description. + +By using our pre-computed transition matrix we not only remote +the need for two levels of nested loops BUT also the need to +perform the checks each time for stepping off the side of the board. + +```perl +my $dist_maps = []; ## Map of distances betw +my $route_maps = []; + +foreach my $start (0..63) { + my @dists = map { $_ == $start ? 0 : -1 } 0..63; + my @routes = map { '' } 0..63; + my $c = 1; + my $d = 0; + while($c<64) { + foreach my $x (0..63) { + next if $dists[$x] != $d; + foreach ( @{$trans->[$x]}) { + next if $T[$_]>=0; + $c++; + $dists[$_] = $d+1; + $routes[$_] = $routes[$x].chr$_; + } + } + $d++; + } + |
