diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-15 19:18:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-15 19:18:42 +0000 |
| commit | 378d9637dbfe4d22435ff779bea4ec1b19c1a087 (patch) | |
| tree | 1684ee4f29b78e6395f4ffb7cc1fd581b7a56adb | |
| parent | 7391bbf55ecd372c952d9478be6ca5d853f06f80 (diff) | |
| parent | 430de9c4385301d4dcce072c09c724bbaf49ebf1 (diff) | |
| download | perlweeklychallenge-club-378d9637dbfe4d22435ff779bea4ec1b19c1a087.tar.gz perlweeklychallenge-club-378d9637dbfe4d22435ff779bea4ec1b19c1a087.tar.bz2 perlweeklychallenge-club-378d9637dbfe4d22435ff779bea4ec1b19c1a087.zip | |
Merge pull request #3529 from Scimon/master
Challenge 1... bit more that 1 line
| -rw-r--r-- | challenge-100/simon-proctor/raku/ch-1.raku | 40 | ||||
| -rw-r--r-- | challenge-100/simon-proctor/raku/ch-2.raku | 33 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-100/simon-proctor/raku/ch-1.raku b/challenge-100/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..9baf46416d --- /dev/null +++ b/challenge-100/simon-proctor/raku/ch-1.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku + +#| Given a time string in am/pm convert to 24hr clock +multi sub MAIN( $t where $t ~~ /^ \d\d ":" \d\d " "? "am"/) { + given $t { + when /^ "12:" (\d\d) " "? "am" $/ { + (S/^ "12:" (\d\d) " "? "am" $/00:$0/).say + } + default { + (S/^ (\d\d ":" \d\d) " "? "am" $/$0/).say + } + } +} + +multi sub MAIN( $t where $t ~~ /^ \d\d ":" \d\d " "? "pm"/) is hidden-from-USAGE { + given $t { + when /^ "12:" (\d\d) " "? "pm" $/ { + (S/^ "12:" (\d\d) " "? "pm" $/12:$0/).say + } + default { + (S/^ (\d\d) ":" (\d\d) " "? "pm" $/{$0+12}:$1/ given $t).say + } + } +} + +#| Given a time in 24hr clock convert to am/pm +multi sub MAIN( $t ) { + given $t { + when /^ "00:" (\d\d) $/ { + (S/^ "00:" (\d\d) $/12:{$1}am/).say + } + when /^ "12:" (\d\d) $/ { + (S/^ "12:" (\d\d) $/12:{$1}pm/).say + } + default { + ($_ ~~ /^ (\d\d) ":" (\d\d) $/); + sprintf( "%02d:%02d%s", $0 > 12 ?? $0-12 !! $0, $1, $0 > 12 ?? "pm" !! "am").say; + } + } +} diff --git a/challenge-100/simon-proctor/raku/ch-2.raku b/challenge-100/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..7d2de18afe --- /dev/null +++ b/challenge-100/simon-proctor/raku/ch-2.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku + +use v6; + +#! Given a series of comma seperated number lists making a triangle (1 -> 2 -> 3 digits long) +#! find the path down the triangle that adds up to the least amount +multi sub MAIN( *@input ) { + my @lines = @input.map( *.split(",") ); + die "Not a triangle" unless [==] 1, |(@lines.map( *.elems ).rotor(2=>-1).map( { @^a[1] - @^a[0] } ) ); + say smallest-route( @lines ); +} + +multi sub MAIN("test") { + use Test; + is( 8, smallest-route( [ [1], [2,4], [6,4,9], [5,1,7,2] ] ) ); + is( 7, smallest-route( [ [3], [3,1], [5,2,3], [4,3,1,3] ] ) ); +} + +multi sub smallest-route( @start ) { + return smallest-route( @start[0], @start[1..*-1], 0 ); +} + +multi sub smallest-route( @head, [], $index ) { + return @head[$index]; +} + +multi sub smallest-route( @head, @rest, $index ) { + my @opts = [ + smallest-route( @rest[0], @rest[1..*-1], $index ), + smallest-route( @rest[0], @rest[1..*-1], $index+1 ) + ]; + return @head[$index] + (@opts[0] < @opts[1] ?? @opts[0] !! @opts[1]); +} |
