aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2022-09-25 15:33:16 -0500
committerUtil <bruce.gray@acm.org>2022-09-25 15:33:16 -0500
commit0bf5a2e01de294e8e523165eaa030c29f4f8238a (patch)
tree0f51f81826355b9f063638565417934a2bb0508f
parent57c4f81e2ca094fe39710cef73c933c2151eca99 (diff)
downloadperlweeklychallenge-club-0bf5a2e01de294e8e523165eaa030c29f4f8238a.tar.gz
perlweeklychallenge-club-0bf5a2e01de294e8e523165eaa030c29f4f8238a.tar.bz2
perlweeklychallenge-club-0bf5a2e01de294e8e523165eaa030c29f4f8238a.zip
Add TWC 183 solutions by Bruce Gray : Raku only.
-rw-r--r--challenge-183/bruce-gray/raku/ch-1.raku16
-rw-r--r--challenge-183/bruce-gray/raku/ch-2.raku46
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-183/bruce-gray/raku/ch-1.raku b/challenge-183/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..ce18b1e250
--- /dev/null
+++ b/challenge-183/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,16 @@
+sub unique-array ( @a ) {
+ return @a.unique( with => &[eqv] );
+}
+
+my @tests =
+ { Input => ( [1,2], [3,4], [5,6], [1,2] ),
+ Expected => ( [1,2], [3,4], [5,6] ) },
+
+ { Input => ( [9,1], [3,7], [2,5], [2,5] ),
+ Expected => ( [9,1], [3,7], [2,5] ) },
+;
+use Test;
+plan +@tests;
+for @tests -> ( :Input(@in), :Expected(@exp) ) {
+ is-deeply unique-array(@in), @exp;
+}
diff --git a/challenge-183/bruce-gray/raku/ch-2.raku b/challenge-183/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..ebd9cb8009
--- /dev/null
+++ b/challenge-183/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,46 @@
+# I would have used .polymod(365), but it won't account for leap years
+sub date-difference ( Str $s1, Str $s2 --> Str ) {
+ my Date ( $d1, $d2 ) = ($s1, $s2).sort.map({ Date.new: $_ });
+
+ my $later-in-year = [lt] map *.Str.substr(4), $d2, $d1;
+
+ my $years = $d2.year - $d1.year - (1 if $later-in-year);
+
+ my $days = $d2 - $d1.later( :$years );
+
+ return join ' ',
+ ( ( "$years year" ~ ('s' if $years > 1) ) if $years ),
+ ( ( "$days day" ~ ('s' if $days > 1) ) if $days ),
+ ;
+}
+
+
+my @tests =
+ ( ( '2019-02-10', '2022-11-01' ), '3 years 264 days' ),
+ ( ( '2020-09-15', '2022-03-29' ), '1 year 195 days' ),
+ ( ( '2019-12-31', '2020-01-01' ), '1 day' ),
+ ( ( '2019-12-01', '2019-12-31' ), '30 days' ),
+ ( ( '2019-12-31', '2020-12-31' ), '1 year' ),
+ ( ( '2019-12-31', '2021-12-31' ), '2 years' ),
+ ( ( '2020-09-15', '2021-09-16' ), '1 year 1 day' ),
+ ( ( '2019-09-15', '2021-09-16' ), '2 years 1 day' ),
+
+ # 2020 was a leap year
+ ( ( '2018-02-28', '2019-02-28' ), '1 year' ),
+ ( ( '2019-02-28', '2020-02-28' ), '1 year' ),
+ ( ( '2020-02-28', '2021-02-28' ), '1 year' ),
+ ( ( '2021-02-28', '2022-02-28' ), '1 year' ),
+
+ ( ( '2018-02-28', '2019-03-01' ), '1 year 1 day' ),
+ ( ( '2019-02-28', '2020-03-01' ), '1 year 2 days' ),
+ ( ( '2020-02-28', '2021-03-01' ), '1 year 1 day' ),
+ ( ( '2021-02-28', '2022-03-01' ), '1 year 1 day' ),
+
+ ( ( '1800-01-01', '1900-01-01' ), '100 years' ),
+
+;
+use Test;
+plan +@tests;
+for @tests -> ( ($s1, $s2), $exp ) {
+ is date-difference($s1, $s2), $exp;
+}