diff options
| -rw-r--r-- | challenge-109/aaronreidsmith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-109/aaronreidsmith/raku/ch-1.raku | 17 | ||||
| -rw-r--r-- | challenge-109/aaronreidsmith/raku/ch-2.raku | 42 |
3 files changed, 60 insertions, 0 deletions
diff --git a/challenge-109/aaronreidsmith/blog.txt b/challenge-109/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..2674317ee8 --- /dev/null +++ b/challenge-109/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-109/ diff --git a/challenge-109/aaronreidsmith/raku/ch-1.raku b/challenge-109/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..7680179ced --- /dev/null +++ b/challenge-109/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku + +sub challenge(Int $n) returns Int { + (2..$n / 2).grep($n %% *).sum; +} + +multi sub MAIN(Int $terms = 20, Bool :$main) { + say (1..$terms).map(&challenge).join(', '); +} + +multi sub MAIN(Bool :$test) { + use Test; + + is((1..20).map(&challenge), (0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21)); + + done-testing; +} diff --git a/challenge-109/aaronreidsmith/raku/ch-2.raku b/challenge-109/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..eb6a1ec8b5 --- /dev/null +++ b/challenge-109/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,42 @@ +#!/usr/bin/env raku + +sub challenge(@nums where @nums.elems == 7) returns Str { + my @solution = @nums + .sort + .permutations + .reverse + .first: -> ($a, $b, $c, $d, $e, $f, $g) { + $a + $b == $b + $c + $d && + $b + $c + $d == $d + $e + $f && + $d + $e + $f == $f + $g + }; + + # If nothing is found, the above returns `[(Any)]` rather than nothing + # This is only because we assign to a variable; otherwise it returns `Nil` + if @solution.elems == 1 { + "Unable to find a solution for given input."; + } else { + (<a b c d e f g> Z @solution) + .map(-> ($key, $value) { "$key = $value" }) + .join("\n"); + } +} + +multi sub MAIN(*@nums where all(@nums) ~~ Int) { + say challenge(@nums); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + ((1, 2, 3, 4, 5, 6, 7), "a = 7\nb = 3\nc = 2\nd = 5\ne = 1\nf = 4\ng = 6"), + ((14, 17, 2, 9, -1, 100, 17), "Unable to find a solution for given input.") + ); + + for @tests -> (@input, $expected) { + is(challenge(@input), $expected); + } + + done-testing; +} |
