diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-19 00:52:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-19 00:52:07 +0000 |
| commit | cc478f57c9d906113a35e428bdb78a1cd17cbc81 (patch) | |
| tree | 6dad2b36796df94ae7972249753009c4a75f86bf | |
| parent | 5a36b4b50ad6fa820870ef50dd589850ed25e849 (diff) | |
| parent | 088c0ddb87b03b5c170f04a6332de86609523072 (diff) | |
| download | perlweeklychallenge-club-cc478f57c9d906113a35e428bdb78a1cd17cbc81.tar.gz perlweeklychallenge-club-cc478f57c9d906113a35e428bdb78a1cd17cbc81.tar.bz2 perlweeklychallenge-club-cc478f57c9d906113a35e428bdb78a1cd17cbc81.zip | |
Merge pull request #3004 from aaronreidsmith/challenge-91
Challenge 91 - Raku
| -rw-r--r-- | challenge-091/aaronreidsmith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-091/aaronreidsmith/raku/ch-1.raku | 56 | ||||
| -rw-r--r-- | challenge-091/aaronreidsmith/raku/ch-2.raku | 51 |
3 files changed, 108 insertions, 0 deletions
diff --git a/challenge-091/aaronreidsmith/blog.txt b/challenge-091/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..ffe0af24ee --- /dev/null +++ b/challenge-091/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-091/
\ No newline at end of file diff --git a/challenge-091/aaronreidsmith/raku/ch-1.raku b/challenge-091/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..ba58d1904b --- /dev/null +++ b/challenge-091/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,56 @@ +#!/usr/bin/env raku + +sub challenge($N) { + my @digits = $N.comb.map(*.Int); + + my ($current-num, $current-count); + my $first = True; + my @output; + for @digits.kv -> $index, $digit { + # If this is the first round, just set our variables and continue + if $first { + $first = False; + $current-num = $digit; + $current-count = 1; + next; + } + + # Otherwise, just keep track of our current digit/count + if $digit == $current-num { + $current-count += 1; + } else { + @output.push($current-count); + @output.push($current-num); + $current-num = $digit; + $current-count = 1; + } + + # We need this to push the last number on, or it will get lost + if $index == @digits.elems - 1 { + @output.push($current-count); + @output.push($current-num); + } + } + + @output.join; +} + +multi sub MAIN($N where $N ~~ Int && $N > 0) { + say challenge($N); +} + +multi sub MAIN(:$test) { + use Test; + + my @tests = ( + (1122234, 21321314), + (2333445, 12332415), + (12345, 1112131415) + ); + + for @tests -> @test { + is(challenge(@test[0]), @test[1]); + } + + done-testing; +} diff --git a/challenge-091/aaronreidsmith/raku/ch-2.raku b/challenge-091/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..1e26586aba --- /dev/null +++ b/challenge-091/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,51 @@ +#!/usr/bin/env raku + +subset PositiveInt of Int where { $_ >= 0 } + +sub challenge(@N) { + my Int $pointer = 0; + my Bool $reached-the-end; + + loop { + given $pointer { + when * < @N.elems - 1 { + my $value = @N[$pointer]; + if $value == 0 { + $reached-the-end = False; + last; + } else { + $pointer += @N[$pointer] + } + } + when * == @N.elems - 1 { + $reached-the-end = True; + last; + } + when * > @N.elems - 1 { + $reached-the-end = False; + last; + } + } + } + + $reached-the-end.Int; +} + +multi sub MAIN(*@N where all(@N) ~~ PositiveInt) { + say challenge(@N); +} + +multi sub MAIN(:$test) { + use Test; + + my @tests = ( + ((1, 2, 1, 2), 1), + ((2, 1, 1, 0, 2), 0) + ); + + for @tests -> @test { + is(challenge(@test[0]), @test[1]); + } + + done-testing; +} |
