diff options
| author | Aaron Smith <asmith@sumologic.com> | 2021-02-20 14:32:23 -0600 |
|---|---|---|
| committer | Aaron Smith <asmith@sumologic.com> | 2021-02-20 14:32:23 -0600 |
| commit | 55e0373f88dbc38ba04d2ae395c7b44c5c6147d7 (patch) | |
| tree | 20e83417736735ba3d8ef6c623fedce9416113ee /challenge-100 | |
| parent | 216a60a4cbd47838e42b055bbc7365d348ee8b8a (diff) | |
| download | perlweeklychallenge-club-55e0373f88dbc38ba04d2ae395c7b44c5c6147d7.tar.gz perlweeklychallenge-club-55e0373f88dbc38ba04d2ae395c7b44c5c6147d7.tar.bz2 perlweeklychallenge-club-55e0373f88dbc38ba04d2ae395c7b44c5c6147d7.zip | |
Challenge 100 - Raku
Diffstat (limited to 'challenge-100')
| -rw-r--r-- | challenge-100/aaronreidsmith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-100/aaronreidsmith/raku/ch-1.raku | 67 | ||||
| -rw-r--r-- | challenge-100/aaronreidsmith/raku/ch-2.raku | 64 |
3 files changed, 132 insertions, 0 deletions
diff --git a/challenge-100/aaronreidsmith/blog.txt b/challenge-100/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..072bf24630 --- /dev/null +++ b/challenge-100/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-100/ diff --git a/challenge-100/aaronreidsmith/raku/ch-1.raku b/challenge-100/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..012169f116 --- /dev/null +++ b/challenge-100/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,67 @@ +#!/usr/bin/env raku + +sub challenge(Str \t) returns Str { + t~~/(\d+)\:(\d+)\s?([a|p]m)?/;my (\h,\m,\q)=$/[*];sprintf('%02d:%02d%s',q??h==12??q eq'am'??0!!h!!h+(12*(q eq'pm'))!!h==0|12??12!!h%12,m,q??''!!h>=12??'pm'!!'am'); +} + +# Only used to describe the above in my blog :) +sub challenge-expanded(Str \t) returns Str { + t ~~ / + (\d+) # One or more digits (should technically use \d ** {2}, but this is shorter + \: # A literal colon character + (\d+) # One or more digits (again, should use \d ** {2}) + \s? # An optional space (to support HH:MMam or HH:MM am) + ([a|p]m)? # An optional 'am' or 'pm' (to support both 12- and 24-hour time) + /; + + my (\h, \m, \q) = $/[*]; + + # The logic in here is the same as above, with added parentheses for clarity + sprintf( + '%02d:%02d%s', + q ?? + (h == 12 ?? + (q eq 'am' ?? 0 !! h) !! + h + (12 * ( q eq 'pm'))) !! + h == 0|12 ?? 12 !! h%12, + m, + q ?? '' !! (h >= 12 ?? 'pm' !! 'am') + ); +} + +multi sub MAIN(Str $time) { + say challenge($time); +} + +multi sub MAIN(Bool :$test, Bool :$verbose = False) { + use Test; + + my @tests = ( + # Provided + ('05:15pm' , '17:15'), + ('05:15 pm', '17:15'), + ('19:15' , '07:15pm'), + # Edge cases + ('00:00' , '12:00am'), + ('12:00am', '00:00'), + ('12:00' , '12:00pm'), + ('12:00pm', '12:00') + ); + + for @tests -> ($input, $expected) { + is(challenge($input), $expected); + # Confirm the expanded version works the same way + is(challenge-expanded($input), $expected); + } + + # If verbose, check that challenge(challenge($t)) == $t for all possible $t + if $verbose { + for (1..12) X (^59) X ('am', 'pm') -> ($hour, $minute, $qualifier) { + my $time = sprintf('%02d:%02d%s', $hour, $minute, $qualifier); + my $converted = challenge($time); + is(challenge($converted), $time); + } + } + + done-testing; +} diff --git a/challenge-100/aaronreidsmith/raku/ch-2.raku b/challenge-100/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..7ffabd79bb --- /dev/null +++ b/challenge-100/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,64 @@ +#!/usr/bin/env raku + +sub challenge(@triangle) { + my @layers = (0..@triangle.end); + my @indices = gather { + for @triangle -> @layer { + take (0..@layer.end).List; + } + } + my @paths = gather { + for ([X] @indices) -> @path { + my @zipped = @path Z @path[1..*]; + my $valid = True; + for @zipped -> ($a, $b) { + if $b < $a || $b > $a + 1 { + $valid = False; + last; + } + } + take @path if $valid; + } + } + my @sums = gather { + my $sum = 0; + for @paths -> @path { + for @layers Z @path -> ($layer, $index) { + $sum += @triangle[$layer][$index]; + } + take $sum; + $sum = 0; + } + } + @sums.min; +} + +multi sub MAIN(*@N where all(@N) ~~ Int) { + my ($index, $size) = (0, 1); + my @triangle; + while $index <= @N.end { + my $end-index = $index + $size; + + my @layer = @N[$index..^$end-index]; + @triangle.push(@layer); + + $index = $end-index; + $size++; + } + say challenge(@triangle); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (((1,), (2, 4), (6, 4, 9), (5, 1, 7, 2)), 8), + (((3,), (3, 1), (5, 2, 3), (4, 3, 1, 3)), 7) + ); + + for @tests -> (@triangle, $expected) { + is(challenge(@triangle), $expected); + } + + done-testing; +} |
