From 5e70fcf6e681215a1d1716353061c3b53cfe108d Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 15 Feb 2021 09:37:36 +0000 Subject: Challenge 1... bit more that 1 line --- challenge-100/simon-proctor/raku/ch-1.raku | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-100/simon-proctor/raku/ch-1.raku 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; + } + } +} -- cgit From 430de9c4385301d4dcce072c09c724bbaf49ebf1 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 15 Feb 2021 16:40:30 +0000 Subject: Challenge 2, recursion time --- challenge-100/simon-proctor/raku/ch-2.raku | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-100/simon-proctor/raku/ch-2.raku 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]); +} -- cgit