diff options
| -rw-r--r-- | challenge-102/aaronreidsmith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-102/aaronreidsmith/raku/ch-1.raku | 61 | ||||
| -rw-r--r-- | challenge-102/aaronreidsmith/raku/ch-2.raku | 37 |
3 files changed, 99 insertions, 0 deletions
diff --git a/challenge-102/aaronreidsmith/blog.txt b/challenge-102/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..1defd55470 --- /dev/null +++ b/challenge-102/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-102/ diff --git a/challenge-102/aaronreidsmith/raku/ch-1.raku b/challenge-102/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..4f7bbf4ed6 --- /dev/null +++ b/challenge-102/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,61 @@ +#!/usr/bin/env raku + +sub digital-root(Int $N) returns Int { + my @digits = $N.comb; + my $digital-root = [+] @digits; + while @digits.elems > 1 { + @digits = $digital-root.comb; + $digital-root = [+] @digits; + } + $digital-root; +} + +sub is-rare(Int $N) returns Bool { + return False if $N.comb.head % 2 != 0; + return False if digital-root($N) ~~ 0|1|3|4|6|7; + + my $reversed = $N.flip.Int; + my $difference = $N - $reversed; + + if $difference >= 0 && $difference.sqrt.narrow ~~ Int { + # Only calculate this if the difference is valid + my $sum = $N + $reversed; + $sum.sqrt.narrow ~~ Int; + } else { + False; + } +} + +sub challenge(Int $N) returns Str { + my $min = ('2' ~ ('0' x $N - 1)).Int; # Rare number can never start with an odd digit. + my $max = ('8' ~ ('9' x $N - 1)).Int; # Rare number can never start with an odd digit. + ($min..$max).hyper.grep(&is-rare).join(', '); +} + +multi sub MAIN(Int $N) { + say challenge($N); +} + +multi sub MAIN(Bool :$test, Bool :$vebose = False) { + use Test; + + my @tests = ( + (1, '2, 8'), + (2, '65'), + (3, '242'), + (4, ''), + (5, '20402, 24642'), + (6, '621770') + ); + + for @tests -> ($N, $expected) { + is(challenge($N), $expected); + } + + # This one is pretty slow, so it is not run by default + if $vebose { + is(challenge(9), '281089082'); + } + + done-testing; +} diff --git a/challenge-102/aaronreidsmith/raku/ch-2.raku b/challenge-102/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..08b69b6822 --- /dev/null +++ b/challenge-102/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,37 @@ +#!/usr/bin/env raku + +sub challenge(Int $N) returns Str { + my @output; + my $index = $N - 1; + while $index >= 0 { + @output[$index] = '#'; + my $position = $index + 1; # Position is 1-based while index is 0-based + for $position.flip.comb.kv -> $offset, $digit { + @output[$index - ($offset + 1)] = $digit; + } + $index -= ($position.chars + 1); + } + @output.join; +} + +multi sub MAIN(Int $N) { + say challenge($N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (1, '#'), + (2, '2#'), + (3, '#3#'), + (10, '#3#5#7#10#'), + (14, '2#4#6#8#11#14#') + ); + + for @tests -> ($N, $expected) { + is(challenge($N), $expected); + } + + done-testing; +} |
