aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlavio Poletti <flavio@polettix.it>2021-03-12 06:53:17 +0100
committerFlavio Poletti <flavio@polettix.it>2021-03-12 06:53:17 +0100
commit85d0d95508cf9d4fca89e0e04213d02f6ce5847c (patch)
tree6c2a3c785cd0809b60617aa4bdf439db4a04fb2d
parentbe0102cdd35ea1f97407f7e935e7702b9f0c02da (diff)
downloadperlweeklychallenge-club-85d0d95508cf9d4fca89e0e04213d02f6ce5847c.tar.gz
perlweeklychallenge-club-85d0d95508cf9d4fca89e0e04213d02f6ce5847c.tar.bz2
perlweeklychallenge-club-85d0d95508cf9d4fca89e0e04213d02f6ce5847c.zip
Add polettix's solution to PWC103
-rw-r--r--challenge-103/polettix/blog.txt1
-rw-r--r--challenge-103/polettix/blog1.txt1
-rw-r--r--challenge-103/polettix/perl/ch-1.pl29
-rw-r--r--challenge-103/polettix/perl/ch-2.pl60
-rw-r--r--challenge-103/polettix/perl/filelist.csv7
5 files changed, 98 insertions, 0 deletions
diff --git a/challenge-103/polettix/blog.txt b/challenge-103/polettix/blog.txt
new file mode 100644
index 0000000000..e028347c93
--- /dev/null
+++ b/challenge-103/polettix/blog.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/03/10/pwc103-chinese-zodiac/
diff --git a/challenge-103/polettix/blog1.txt b/challenge-103/polettix/blog1.txt
new file mode 100644
index 0000000000..c0c71bf615
--- /dev/null
+++ b/challenge-103/polettix/blog1.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/03/11/pwc103-what-s-playing/
diff --git a/challenge-103/polettix/perl/ch-1.pl b/challenge-103/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..04e7986652
--- /dev/null
+++ b/challenge-103/polettix/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+
+sub chinese_zodiac ($year) {
+ my ($s, $y, $acbc) = $year =~ m{
+ \A \s*
+ (-?) \s*
+ ([1-9]\d*) \s*
+ ((?:ad|bc)?)
+ \s* \z
+ }imxs;
+ die "invalid input date '$year'\n"
+ if (! defined $y) || ($s eq '-' && length $acbc);
+ $year = $s eq '-' || lc($acbc) eq 'bc' ? -$y : $y;
+ my $r = $year > 0 ? (($year + 56) % 60) : 59 - ((2 - $year) % 60);
+ my $yin_yang = (qw< Yang Yin >)[$r % 2];
+ my $element = (qw< Wood Fire Earth Metal Water >)[int($r / 2) % 5];
+ my $animal = (qw< Rat Ox Tiger Rabbit Dragon Snake
+ Horse Goat Monkey Rooster Dog Pig >)[$r % 12];
+ return ($yin_yang, $element, $animal);
+}
+
+my $y = "@ARGV" || 1972;
+my ($yin_yang, $element, $animal) = chinese_zodiac($y);
+say "$element $animal";
+say {*STDERR} "$yin_yang $element $animal";
diff --git a/challenge-103/polettix/perl/ch-2.pl b/challenge-103/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..6a1a63112c
--- /dev/null
+++ b/challenge-103/polettix/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+use autodie;
+use File::Spec::Functions qw< splitpath catpath >;
+
+sub what_s_playing ($start, $now, $file) {
+ my $tracks = load_tracks_list($file);
+ my $offset = 1000 * ($now - $start);
+ my $current_title;
+ OUTER:
+ while ('necessary') {
+ my $period = 0;
+ for my $track ($tracks->@*) {
+ my $duration = $track->{duration};
+ if ($offset <= $duration) {
+ $current_title = $track->{title};
+ last OUTER;
+ }
+ $offset -= $duration;
+ $period += $duration;
+ }
+ $offset %= $period;
+ }
+
+ my $ms = $offset % 1000;
+ $offset = int($offset / 1000);
+ my $s = $offset % 60;
+ $offset = int($offset / 60);
+ my $m = $offset % 60;
+ $offset = int($offset / 60);
+ my $current_position = sprintf '%02d:%02d:%02d.%03d', $offset, $m, $s, $ms;
+
+ return {position => $current_position, title => $current_title};
+}
+
+sub load_tracks_list ($file) {
+ open my $fh, '<', $file;
+ my @lines = map {
+ chomp;
+ my ($duration, $title) = split m{,}mxs, $_, 2;
+ $duration =~ s{\A\s+|\s+\z}{}gmxs;
+ substr $duration, -3, 3, '000' if $ENV{CUT_MILLISECONDS};
+ {duration => $duration, title => $title};
+ } <$fh>;
+ return \@lines;
+}
+
+sub default_args {
+ my ($v, $ds, $f) = splitpath(__FILE__);
+ my $file = catpath($v, $ds, 'filelist.csv');
+ return (1606134123, 1614591276, $file);
+}
+
+my @args = @ARGV ? @ARGV : default_args();
+my $wp = what_s_playing(@args);
+say $wp->{title};
+say $wp->{position};
diff --git a/challenge-103/polettix/perl/filelist.csv b/challenge-103/polettix/perl/filelist.csv
new file mode 100644
index 0000000000..9428b93004
--- /dev/null
+++ b/challenge-103/polettix/perl/filelist.csv
@@ -0,0 +1,7 @@
+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)"