aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-03-09 17:40:26 -0500
committerDave Jacoby <jacoby.david@gmail.com>2021-03-09 17:40:26 -0500
commit962d06e998d0fbe8dc3a7513b5e67f20ecad1aa4 (patch)
treec536f5d59582f99b5e722cb4f2f14206cdc191ef
parentdd678c5e9cab8a8021d6eea40f059fe0fdbdb340 (diff)
downloadperlweeklychallenge-club-962d06e998d0fbe8dc3a7513b5e67f20ecad1aa4.tar.gz
perlweeklychallenge-club-962d06e998d0fbe8dc3a7513b5e67f20ecad1aa4.tar.bz2
perlweeklychallenge-club-962d06e998d0fbe8dc3a7513b5e67f20ecad1aa4.zip
Challenge 103 Solutions or so
-rw-r--r--challenge-103/dave-jacoby/perl/ch-1.pl94
-rw-r--r--challenge-103/dave-jacoby/perl/ch-2.pl86
-rw-r--r--challenge-103/dave-jacoby/perl/input.csv7
3 files changed, 187 insertions, 0 deletions
diff --git a/challenge-103/dave-jacoby/perl/ch-1.pl b/challenge-103/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..b5b0c3ceb0
--- /dev/null
+++ b/challenge-103/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,94 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ postderef say signatures state };
+no warnings qw{ experimental };
+
+# Do Not Write Your Own Date and Time Manipulation Code!
+# Do Not Write Your Own Date and Time Manipulation Code!
+# Do Not Write Your Own Date and Time Manipulation Code!
+# Do Not Write Your Own Date and Time Manipulation Code!
+# Do Not Write Your Own Date and Time Manipulation Code!
+# Do Not Write Your Own Date and Time Manipulation Code!
+# -- Dave Rolsky
+
+# Time is broken.
+
+use Carp;
+use DateTime;
+use DateTime::Calendar::Chinese
+ ; # which requires DateTime::Astro, which requires MPFR or won't build
+
+# we can specify a specific year, or we can get a whole cycle
+my ($year) = @ARGV;
+if ( defined $year ) {
+ croak 'Not A Year' unless $year !~ /\D/;
+ my ( $year, $animal, $branch, $stem, $name, $element ) =
+ chinese_zodiac($year);
+ say join " ", "\t", $year, "\t", $element, $animal;
+}
+
+else {
+ # Show The Current Cycle
+ for my $y ( 1984 .. 2043 ) {
+ my ( $year, $animal, $branch, $stem, $name, $element ) =
+ chinese_zodiac($y);
+ say join " ", "\t", $year, "\t", $element, $animal;
+ }
+}
+exit;
+
+sub chinese_zodiac ( $greg_year ) {
+
+ # there is a problem I'm ignoring here.
+
+ # chinese new year is not bound specifically to a point in the
+ # planet's orbit around the local star. it floats, and occurs
+ # somewhere between mid-January and mid-February by the Gregorian
+ # calendar.
+
+ # this means that, if run on Jan 2 2021, we MIGHT expect to see
+ # that it's Metal Ox, but it was still Metal Rat (which is the
+ # name of an effects pedal from ProCo, IIRC, and implies a series
+ # of SF novels from Harry Harrison).
+
+ # this code *should* respect the truth and not the failed expectations
+ # of the user, but any code that goes from first principles would
+ # likely blow it for about the first month of the year
+
+ my $dt = DateTime->now();
+ $dt->set_year($greg_year);
+ my $chdt = DateTime::Calendar::Chinese->from_object( object => $dt );
+ $chdt->set_time_zone("Asia/Hong_Kong");
+
+ my $animal = ucfirst $chdt->zodiac_animal;
+ my $branch = ucfirst $chdt->terrestrial_branch_py;
+ my $stem = ucfirst $chdt->celestial_stem_py;
+ my $name = ucfirst $chdt->year_name_py;
+ my $element = translate_element($stem);
+ return $greg_year, $animal, $branch, $stem, $name, $element;
+}
+
+sub translate_element ( $stem ) {
+
+ # There's also Yin and Yang, which we were not
+ # told to identify, but explains the two results
+ # in the following hash
+
+ # Wood, Fire, Earth, Metal, Water
+ my %elements = (
+ Jia3 => 'Wood',
+ Yi3 => 'Wood',
+ Bing3 => 'Fire',
+ Ding1 => 'Fire',
+ Ji3 => 'Earth',
+ Wu4 => 'Earth',
+ Geng1 => 'Metal',
+ Xin1 => 'Metal',
+ Gui3 => 'Water',
+ Ren2 => 'Water',
+ );
+ return $elements{$stem};
+}
+
diff --git a/challenge-103/dave-jacoby/perl/ch-2.pl b/challenge-103/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..41b9500283
--- /dev/null
+++ b/challenge-103/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,86 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ postderef say signatures state };
+no warnings qw{ experimental };
+
+use Carp;
+use Text::CSV qw( csv );
+use Getopt::Long;
+my $starttime = 1606134123; # 2020-11-23 12:22:03
+my $currtime = 1614591276; # 2021-03-01 09:34:36
+my $file = 'input.csv';
+
+my $np = now_playing( $starttime, $currtime, $file );
+say join "\n\t", $np->@*;
+
+sub now_playing ( $starttime, $currtime, $file ) {
+ croak 'Bad Times' unless $currtime > $starttime;
+ croak 'No Input File' unless -f $file;
+
+ # $starttime is when the streaming started
+ # $currtime is the time during the stream we're asking about
+ # $endtime is the total time in seconds that the streamer
+ # has been running
+ my $endtime = $currtime - $starttime;
+ my @data = get_data($file);
+
+ # 4:50pm - Restate my assumptions
+ # One: Mathematics is the language of nature.
+ # Two: Everything around us can be represented
+ # and understood through numbers.
+ # Three: The streamer adds no time between recordings,
+ # nor crossfades between trackss, so there is no
+ # time unaccounted for between tracks
+ # Four: The streamer will get to the end of the list and
+ # start all over again
+ # Five: Pi is a movie that every developer of a certain age
+ # will recognize immediately
+ # Six: Current time will be higher than start time, meaning
+ # we're not querying the past
+ # Seven: The episodes in the CSV are roughly a half-hour each
+
+ # Hypothesis:
+ # We can subtract the start time from the time in
+ # question to find the total number of seconds that
+ # the streamer has played, then go through the whole
+ # playlist over and over until the current total is
+ # less than the run time, but the current total plus
+ # the next media file would be greater than the run
+ # time. This would indicate that this is the currently
+ # streaming file
+
+ my $ctime = 0;
+ while (1) {
+ for my $c ( 0 .. -1 + scalar @data ) {
+ my $row = $data[$c];
+ my ( $mill, $name ) = $row->@*;
+ my $length = $mill / 1000;
+ my $c2 = $ctime;
+ $ctime += $length;
+ if ( $ctime >= $endtime && $c2 <= $endtime ) {
+ my $time = int $endtime - $c2;
+ my $formatted = join ':',
+ ( int $time / 60 ),
+ ( sprintf '%02d', $time % 60 );
+ my @output = ( $row->[1], $formatted );
+ return \@output;
+ }
+ }
+ }
+ croak 'This should never happen';
+}
+
+# getting to the CSV is a separate function for ease
+sub get_data ($file=undef) {
+ $file //= 'input.csv';
+ if ( -f $file ) {
+ my $data = csv( in => $file );
+ return $data->@*;
+ }
+ else {
+ croak 'no data';
+ }
+}
+
diff --git a/challenge-103/dave-jacoby/perl/input.csv b/challenge-103/dave-jacoby/perl/input.csv
new file mode 100644
index 0000000000..0b652b0a37
--- /dev/null
+++ b/challenge-103/dave-jacoby/perl/input.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)" \ No newline at end of file