aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-02-16 16:51:19 -0500
committerDave Jacoby <jacoby.david@gmail.com>2021-02-16 16:51:19 -0500
commitf495ec7463ca883b5282b04db98f563712984dac (patch)
tree48bb6078f8968e4fce3e8954eb2856e3576a4b43
parentcbd68f93a19d90e144eea3fc25a2af68b362d036 (diff)
downloadperlweeklychallenge-club-f495ec7463ca883b5282b04db98f563712984dac.tar.gz
perlweeklychallenge-club-f495ec7463ca883b5282b04db98f563712984dac.tar.bz2
perlweeklychallenge-club-f495ec7463ca883b5282b04db98f563712984dac.zip
Challenge 100
-rw-r--r--challenge-100/dave-jacoby/node/ch-2.js45
-rw-r--r--challenge-100/dave-jacoby/perl/ch-1.pl70
-rw-r--r--challenge-100/dave-jacoby/perl/ch-2.pl48
3 files changed, 163 insertions, 0 deletions
diff --git a/challenge-100/dave-jacoby/node/ch-2.js b/challenge-100/dave-jacoby/node/ch-2.js
new file mode 100644
index 0000000000..5f023375c6
--- /dev/null
+++ b/challenge-100/dave-jacoby/node/ch-2.js
@@ -0,0 +1,45 @@
+"use strict";
+
+var input = [];
+input.push([[1], [2, 4], [6, 4, 9], [5, 1, 7, 2]]);
+input.push([[3], [3, 1], [5, 2, 3], [4, 3, 1, 3]]);
+
+for (let i = 0; i < input.length; i++) {
+ triangle_sum(input[i]);
+}
+
+function triangle_sum(input) {
+ let results = triangle(input, 0, 0, []).sort(function(a, b) { return a - b });
+ console.log(input);
+ console.log(results[0]);
+ console.log("");
+}
+
+function triangle(input, x, y, path) {
+ if (x > 5) {
+ return;
+ }
+ let output = [];
+
+ if ("undefined" === typeof input[x]) {
+ output.push(path.reduce((a, b) => a + b, 0));
+ } else {
+ let v = input[x][y];
+ let next_path = [...path];
+ next_path.push(v);
+
+ output.push(...triangle(input, x + 1, y, next_path));
+ output.push(...triangle(input, x + 1, y + 1, next_path));
+ }
+ return output;
+}
+
+
+// [ [ 1 ], [ 2, 4 ], [ 6, 4, 9 ], [ 5, 1, 7, 2 ] ]
+// 8
+//
+// [ [ 3 ], [ 3, 1 ], [ 5, 2, 3 ], [ 4, 3, 1, 3 ] ]
+// 7
+
+
+
diff --git a/challenge-100/dave-jacoby/perl/ch-1.pl b/challenge-100/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..df96c5ef42
--- /dev/null
+++ b/challenge-100/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental::postderef experimental::signatures };
+
+# You are given a time (12 hour / 24 hour).
+#
+# Write a script to convert the given time from 12 hour format to 24 hour format and vice versa.
+#
+# Ideally we expect a one-liner.
+
+my @times =
+ sort { $b =~ /m/ <=> $a =~ /m/ }
+ sort
+
+ (
+ '5:14', '05:15 pm', '05:15pm', '05:15 am', '05:15am', '17:15',
+ '19:15', '07:15 pm', '07:15pm', '12:00am', '12:00pm', '00:00',
+ '12:00', '24:00', '7:7am', '7:7pm'
+ );
+
+for my $time (@times) {
+ say join "\t", '', $time, ' <=> ', switch_time($time);
+ say '';
+}
+
+# 12pm is noon
+# 12am is midnight
+sub switch_time ( $time ) {
+ my $out = '';
+
+ # 12-hour time
+ if ( $time =~ /m$/mix ) {
+ my ( $hr, $min, $ampm ) = $time =~ /(\d+):(\d+)\s*(am|pm|)/mix;
+ $out = join ':',
+ (
+ $ampm eq 'am'
+ ? (
+ $hr == 0 ? '00': $hr
+ )
+ : (
+ $hr == '12'
+ ? sprintf '%02d',
+ $min
+ : sprintf '%02d',
+ $hr + 12
+ )
+ ),
+ ( sprintf '%02d', $min );
+ }
+
+ # 24-hour time
+ else {
+ my ( $hr, $min ) = $time =~ /(\d+):(\d+)/mix;
+ $out = join '',
+ (
+ $hr == 0 || $hr == 24
+ ? 12
+ : ( $hr > 12 ? $hr % 12 : $hr )
+ ),
+ (':'),
+ ( sprintf '%02d', $min ),
+ ( $hr < 12 ? 'am' : 'pm' );
+ }
+ return $out;
+}
+
+
diff --git a/challenge-100/dave-jacoby/perl/ch-2.pl b/challenge-100/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..15d761b17f
--- /dev/null
+++ b/challenge-100/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental::postderef experimental::signatures };
+
+use List::Util qw{sum};
+use JSON;
+my $json = JSON->new;
+my $p = JSON->new->pretty->canonical;
+
+my @input;
+push @input, [ [1], [ 2, 4 ], [ 6, 4, 9 ], [ 5, 1, 7, 2 ] ];
+push @input, [ [3], [ 3, 1 ], [ 5, 2, 3 ], [ 4, 3, 1, 3 ] ];
+
+for my $input (@input) {
+ triangle_sum($input);
+}
+
+sub triangle_sum ( $input ) {
+ my ($short) =
+ sort { $a->{sum} <=> $b->{sum} } triangle($input);
+ say qq{ sum: $short->{sum} };
+ say q{ path: } . join ' ', $short->{path}->@*;
+ for my $i ( $input->@* ) {
+ say join ' ', ' ', $i->@*;
+ }
+ say '';
+}
+
+sub triangle ( $input, $x = 0, $y = 0, @path ) {
+ my @output;
+
+ # if not a leaf, go left and right
+ if ( defined $input->[$x][$y] ) {
+ push @output, triangle( $input, $x + 1, $y, @path, $y );
+ push @output, triangle( $input, $x + 1, $y + 1, @path, $y );
+ }
+
+ # if a leaf, find the sum, find the path, and return
+ else {
+ my @ind = map { $path[$_] } 0 .. $x - 1;
+ my $sum = sum map { $input->[$_][ $path[$_] ] } 0 .. $x - 1;
+ push @output, { sum => $sum, path => \@ind, };
+ }
+ return @output;
+}