aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-103/stuart-little/perl/ch-1.pl23
-rwxr-xr-xchallenge-103/stuart-little/perl/ch-2.pl57
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-103/stuart-little/perl/ch-1.pl b/challenge-103/stuart-little/perl/ch-1.pl
new file mode 100755
index 0000000000..9f505ff958
--- /dev/null
+++ b/challenge-103/stuart-little/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+use warnings;
+use v5.12;
+
+# run <script> <year>
+
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+sub animal($animals,$year,$base) {
+ @{$animals}[($year - $base) % (scalar @{$animals})]
+}
+
+sub elt($elts,$year,$base) {
+ @{$elts}[int((($year - $base) % (2*(scalar @{$elts})))/2)]
+}
+
+my @animals=qw(Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog Pig);
+my @elts=qw(Wood Fire Earth Metal Water);
+my $year=$ARGV[0];
+
+say qq|${\ do {elt(\@elts,$year,1924)}} ${\ do {animal(\@animals,$year,1924)}}|;
+
diff --git a/challenge-103/stuart-little/perl/ch-2.pl b/challenge-103/stuart-little/perl/ch-2.pl
new file mode 100755
index 0000000000..d4ffa602aa
--- /dev/null
+++ b/challenge-103/stuart-little/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+use warnings;
+use v5.12;
+
+# run <script> <start time as unix epoch> <current time as unix epoch> <path-to-csv>
+
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+use POSIX qw(round);
+
+sub fromCSV($path) {
+ my @res;
+ open(my $fh, "<", $path)
+ or die "Failed to open file: $!\n";
+ while(<$fh>) {
+ chomp;
+ my @tf = split(/,/,$_);
+ push @res, \@tf;
+ }
+ return \@res;
+}
+
+sub totPlayTime($files) {
+ my $sum=0;
+ for (@{$files}) {
+ $sum+=$_->[0];
+ }
+ return $sum;
+}
+
+sub cvToTime($millisecs) {
+ my $totSecs=round($millisecs/1000);
+ my $s = $totSecs % 60;
+ my $m = (int($totSecs/60)) % 60;
+ my $h = int(($totSecs - $m*60 - $s)/60**2);
+ return ($h,$m,$s);
+}
+
+sub whichPlaying($tdiff,$files) {
+ ($tdiff <= $files->[0]->[0]) && return $files->[0]->[1];
+ my @rest=$files->@[1..scalar @$files-1];
+ return whichPlaying($tdiff - $files->[0]->[0], \@rest);
+}
+
+sub wherePlaying($tdiff,$files) {
+ cvToTime($tdiff % totPlayTime($files))
+}
+
+my @files = @{fromCSV($ARGV[2])};
+my $tdiff = ($ARGV[1]-$ARGV[0])*1000 % totPlayTime(\@files);
+
+my ($h,$m,$s) = wherePlaying($tdiff,\@files);
+my $playingFile = whichPlaying($tdiff,\@files);
+
+say qq|\nPlaying: $playingFile\n\nAt: $h:$m:$s|;
+