aboutsummaryrefslogtreecommitdiff
path: root/challenge-103
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-03-15 05:50:38 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-03-15 05:50:38 +0000
commit4443d6b7b2d43a7e7fa835bd3156cc5dd2f99929 (patch)
treeb12898256fb7e635697ff7c2b4776c37308450e9 /challenge-103
parentc6a66605efa5e00041eff30511e62a0b58aea777 (diff)
downloadperlweeklychallenge-club-4443d6b7b2d43a7e7fa835bd3156cc5dd2f99929.tar.gz
perlweeklychallenge-club-4443d6b7b2d43a7e7fa835bd3156cc5dd2f99929.tar.bz2
perlweeklychallenge-club-4443d6b7b2d43a7e7fa835bd3156cc5dd2f99929.zip
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-103')
-rw-r--r--challenge-103/pete-houston/perl/ch-1.pl27
-rw-r--r--challenge-103/pete-houston/perl/ch-2.pl61
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-103/pete-houston/perl/ch-1.pl b/challenge-103/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..123373bc9c
--- /dev/null
+++ b/challenge-103/pete-houston/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 10301.pl
+#
+# USAGE: ./10301.pl YEAR
+#
+# DESCRIPTION: Output Chinese Zodiac element and animal for given
+# Gregorian year.
+#
+# NOTES: All years are assumed to be CE. Zodiac is as at 31st Dec.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 08/03/21
+#===============================================================================
+
+use strict;
+use warnings;
+
+my @elements = qw/Metal Water Wood Fire Earth/;
+my @animals = qw/Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat/;
+my $year = $ARGV[0];
+my $element = $elements[int ($year / 2) % 5];
+my $animal = $animals[$year % 12];
+
+print "$element $animal\n";
diff --git a/challenge-103/pete-houston/perl/ch-2.pl b/challenge-103/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..adf91d4819
--- /dev/null
+++ b/challenge-103/pete-houston/perl/ch-2.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 10302.pl
+#
+# USAGE: ./10302.pl STARTTIME [ NOW ] FILE
+#
+# DESCRIPTION: What's playing now?
+#
+# OPTIONS: If the 2nd arg is omitted, uses the current time.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 08/03/21
+#===============================================================================
+
+use strict;
+use warnings;
+
+my ($start, $now, $file) = get_args ();
+
+my @tracks = load_tracks ($file);
+my $duration = (1000 * ($now - $start)) % $tracks[-1]->{finish};
+my $trackstart = 0;
+
+for my $track (@tracks) {
+ if ($track->{finish} < $duration) {
+ $trackstart = $track->{finish};
+ next;
+ }
+ output ($track->{title}, $duration - $trackstart);
+ last;
+}
+
+sub load_tracks {
+ my $fname = shift;
+ open my $fh, '<', $fname or die "Cannot open $fname for reading: $!";
+ my ($fin, @t) = 0;
+ while (my $line = <$fh>) {
+ my ($len, $name) = split /,/, $line, 2;
+ $fin += $len;
+ push @t, { finish => $fin, title => $name };
+ }
+ return @t;
+}
+
+sub get_args {
+ if (3 == @ARGV) {
+ return @ARGV;
+ } elsif (2 == @ARGV) {
+ return ($ARGV[0], time, $ARGV[1]);
+ } else {
+ die "Usage: $0 STARTTIME [ NOW ] FILE\n\n";
+ }
+}
+
+sub output {
+ my ($t, $s) = @_;
+ $s /= 1000;
+ printf "%s%2.2i:%2.2i:%2.2i\n", $t, $s / 3600, ($s / 60 % 60), $s % 60;
+}