From 4443d6b7b2d43a7e7fa835bd3156cc5dd2f99929 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 15 Mar 2021 05:50:38 +0000 Subject: - Added solutions by Pete Houston. --- challenge-103/pete-houston/perl/ch-1.pl | 27 +++++++++++++++ challenge-103/pete-houston/perl/ch-2.pl | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 challenge-103/pete-houston/perl/ch-1.pl create mode 100644 challenge-103/pete-houston/perl/ch-2.pl (limited to 'challenge-103') 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; +} -- cgit