aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Smith <asmith@sumologic.com>2021-03-13 13:18:18 -0600
committerAaron Smith <asmith@sumologic.com>2021-03-13 13:18:48 -0600
commit9997a2064908b4e46603294be6583fd1c0175294 (patch)
treea212d1a2ce47a45544b33b0bd50e213fb63d33df
parent4ca90aa3b8a785fe2e2d0b64524c42c4015f11bf (diff)
downloadperlweeklychallenge-club-9997a2064908b4e46603294be6583fd1c0175294.tar.gz
perlweeklychallenge-club-9997a2064908b4e46603294be6583fd1c0175294.tar.bz2
perlweeklychallenge-club-9997a2064908b4e46603294be6583fd1c0175294.zip
Challenge 103 - Raku
-rw-r--r--challenge-103/aaronreidsmith/blog.txt1
-rw-r--r--challenge-103/aaronreidsmith/raku/ch-1.raku54
-rw-r--r--challenge-103/aaronreidsmith/raku/ch-2.raku67
3 files changed, 122 insertions, 0 deletions
diff --git a/challenge-103/aaronreidsmith/blog.txt b/challenge-103/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..bb1e108d32
--- /dev/null
+++ b/challenge-103/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-103/
diff --git a/challenge-103/aaronreidsmith/raku/ch-1.raku b/challenge-103/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..e5ca89497d
--- /dev/null
+++ b/challenge-103/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,54 @@
+#!/usr/bin/env raku
+
+sub challenge(Int $year) returns Str {
+ constant $root-year = 1924; # From Wikipedia
+
+ my $difference = $year - $root-year;
+ my $element-difference = $difference < 0 ?? $difference + 10 !! $difference;
+ my $animal-difference = $difference < 0 ?? $difference + 12 !! $difference;
+
+ my $element = do given $element-difference % 10 {
+ when 0|1 { 'Wood' }
+ when 2|3 { 'Fire' }
+ when 4|5 { 'Earth' }
+ when 6|7 { 'Metal' }
+ when 8|9 { 'Water' }
+ }
+ my $animal = do given $animal-difference % 12 {
+ when 0 { 'Rat' }
+ when 1 { 'Ox' }
+ when 2 { 'Tiger' }
+ when 3 { 'Rabbit' }
+ when 4 { 'Dragon' }
+ when 5 { 'Snake' }
+ when 6 { 'Horse' }
+ when 7 { 'Goat' }
+ when 8 { 'Monkey' }
+ when 9 { 'Rooster' }
+ when 10 { 'Dog' }
+ when 11 { 'Pig' }
+ }
+
+ "$element $animal";
+}
+
+multi sub MAIN(Int $year) {
+ say challenge($year);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (2017, 'Fire Rooster'),
+ (1938, 'Earth Tiger'),
+ (1924, 'Wood Rat'), # Should work for root case
+ (1923, 'Water Pig') # Should work for years < 1924
+ );
+
+ for @tests -> ($year, $expected) {
+ is(challenge($year), $expected);
+ }
+
+ done-testing;
+}
diff --git a/challenge-103/aaronreidsmith/raku/ch-2.raku b/challenge-103/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..6b5e4c92fc
--- /dev/null
+++ b/challenge-103/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,67 @@
+#!/usr/bin/env raku
+
+use Text::CSV; # imports `csv` function
+
+sub challenge(Int $start-time, Int $current-time, Str $file-name) returns Str {
+ my @playlist = csv(in => $file-name);
+ my $playlist-length = @playlist[*;0].sum;
+ my $playlist-position = ($current-time - $start-time) * 1000 % $playlist-length;
+
+ my ($track, $timestamp);
+ for @playlist -> ($track-length, $track-name) {
+ # If we are <= the playlist position, skip to the next track
+ if $track-length <= $playlist-position {
+ $playlist-position -= $track-length;
+ next;
+ }
+
+ # We know we are in the right track now, so find how far in we are
+ $track = $track-name;
+ my $total-seconds = ($playlist-position / 1000).Int;
+ my $hour = ($total-seconds / 3600).Int;
+ my $minutes = ($total-seconds % 3600 / 60).Int;
+ my $seconds = $total-seconds % 60;
+ $timestamp = sprintf('%02d:%02d:%02d', $hour, $minutes, $seconds);
+ last;
+ }
+
+ "$track\n$timestamp";
+}
+
+multi sub MAIN(Int $start-time, Int $current-time, Str $file-name) {
+ say challenge($start-time, $current-time, $file-name);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ # Write out test file.
+ my @rows = (
+ ('1709363', 'Les Miserables Episode 1: The Bishop (broadcast date: 1937-07-23)'),
+ ('1723781', 'Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)'),
+ ('1723781', 'Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)'),
+ ('1678356', 'Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)'),
+ ('1646043', 'Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)'),
+ ('1714640', 'Les Miserables Episode 6: The Barricade (broadcast date: 1937-08-27)'),
+ ('1714640', 'Les Miserables Episode 7: Conclusion (broadcast date: 1937-09-03)')
+ );
+ my $test-file = 'tmp.csv';
+ my $csv = Text::CSV.new;
+ my $fh = open($test-file, :w);
+ $csv.say($fh, $_) for @rows;
+ $fh.close;
+
+ my @tests = (
+ (1606134123, 1614591276, $test-file, "Les Miserables Episode 1: The Bishop (broadcast date: 1937-07-23)\n00:10:24"),
+ (1606134123, 1614592880, $test-file, "Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)\n00:08:38")
+ );
+
+ for @tests -> ($start-time, $current-time, $file-name, $expected) {
+ is(challenge($start-time, $current-time, $file-name), $expected);
+ }
+
+ # Delete test file
+ unlink($test-file);
+
+ done-testing;
+}