aboutsummaryrefslogtreecommitdiff
path: root/challenge-100
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-02-16 18:11:49 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-02-16 18:11:49 +0000
commit21e60474fc0c9a38f930a493a0d9ce0c17fd288f (patch)
treec23e55c4a245dc774c9ca8cd7bfd2a87f0405a13 /challenge-100
parent9a23ffa16ec7f6689859378ed524609ca2a1de38 (diff)
downloadperlweeklychallenge-club-21e60474fc0c9a38f930a493a0d9ce0c17fd288f.tar.gz
perlweeklychallenge-club-21e60474fc0c9a38f930a493a0d9ce0c17fd288f.tar.bz2
perlweeklychallenge-club-21e60474fc0c9a38f930a493a0d9ce0c17fd288f.zip
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-100')
-rw-r--r--challenge-100/pete-houston/perl/ch-1.pl65
-rw-r--r--challenge-100/pete-houston/perl/ch-2.pl78
2 files changed, 143 insertions, 0 deletions
diff --git a/challenge-100/pete-houston/perl/ch-1.pl b/challenge-100/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..71393d1fc2
--- /dev/null
+++ b/challenge-100/pete-houston/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 10001.pl
+#
+# USAGE: ./10001.pl [ STRING ]
+#
+# DESCRIPTION: Analyses input to determine if it is 12 or 24 hour clock
+# and converts to the other. Runs tests if no argument.
+#
+# REQUIREMENTS: Test::More for the test suite.
+# NOTES: The leading zero pad comes from the spec examples.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 15/02/21
+#===============================================================================
+
+use strict;
+use warnings;
+use Test::More;
+
+if (0 < @ARGV) {
+ print clockit($ARGV[0]) . "\n";
+ exit;
+}
+
+sub clockit {
+ my $in = shift;
+ my ($h, $m, $i) = $in =~ /(\d{2})(:\d{2})(\s?[ap]m)?/i;
+ if ($i) {
+ # 12 to 24
+ if ($i =~ /am/i) {
+ $h = '00' if $h eq '12';
+ } elsif ($h < 12) {
+ $h += 12;
+ }
+ return "$h$m";
+ } else {
+ # 24 to 12
+ $i = $h > 11 ? 'pm' : 'am';
+ $h %= 12 if $h > 12;
+ $h = '12' if $h eq '00';
+ # Spec (examples) show padded leading zero
+ return sprintf "%2.2i$m$i", $h;
+ }
+}
+
+my @tests = (
+ { in => '05:15 pm', out => '17:15' },
+ { in => '05:15pm', out => '17:15' },
+ { in => '05:15am', out => '05:15' },
+ { in => '12:15am', out => '00:15' },
+ { in => '12:15pm', out => '12:15' },
+ { in => '00:15', out => '12:15am' },
+ { in => '05:59', out => '05:59am' },
+ { in => '12:15', out => '12:15pm' },
+ { in => '13:42', out => '01:42pm' },
+);
+
+plan tests => scalar @tests;
+
+for my $t (@tests) {
+ is clockit($t->{in}), $t->{out}, "$t->{in} becomes $t->{out}";
+}
diff --git a/challenge-100/pete-houston/perl/ch-2.pl b/challenge-100/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..e34b3b9afd
--- /dev/null
+++ b/challenge-100/pete-houston/perl/ch-2.pl
@@ -0,0 +1,78 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 10002.pl
+#
+# USAGE: ./10002.pl < input_file
+#
+# DESCRIPTION: Minimum path sum down the triangle. Expects the triangle
+# to be on STDIN with one row per line. Each entry is expected to be a
+# whole number, separated from neighbours by non-digits.
+# Example input_file:
+#
+# 1
+# 2 4
+# 6 4 9
+# 5 1 7 2
+#
+# REQUIREMENTS: Perl 5.10, Class::Tiny, Ref::Util, List::Util
+# NOTES: Perhaps not the most efficient, but I've been looking
+# for an excuse to try Class::Tiny, so here it is.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 15/02/21
+#===============================================================================
+
+use strict;
+use warnings;
+use 5.010;
+
+package Node;
+
+use Class::Tiny qw/lchild rchild value minval/;
+use Ref::Util qw/is_ref is_arrayref/;
+use List::Util 'min';
+
+sub BUILDARGS {
+ my ($class, $init, $r, $c, $lastchild) = @_;
+ if (is_arrayref ($init)) {
+ # Buld tree from AoA
+ $r //= 0;
+ $c //= 0;
+ my $self = Node->new ($init->[$r][$c]);
+
+ # Add children if we are not the bottom row
+ if ($r < $#$init) {
+ $self->lchild (
+ $lastchild //
+ Node->new ($init, $r + 1, $c));
+ $self->rchild (Node->new ($init, $r + 1, $c + 1,
+ $self->lchild->rchild));
+ }
+ return $self;
+ } elsif (!is_ref ($init)) {
+ # Set value from scalar
+ return { value => $init };
+ }
+ return {};
+}
+
+# Retrieve or calculate the minimum path downwards from here.
+sub minsum {
+ my $self = shift;
+ unless (defined $self->minval) {
+ my $sum = $self->value;
+ $sum += min ($self->lchild->minsum, $self->rchild->minsum) if
+ defined $self->lchild;
+ $self->minval ($sum)
+ }
+ return $self->minval;
+}
+
+package main;
+
+my @aoa = map { [/([0-9]+)/g] } <STDIN>;
+my $root = Node->new (\@aoa);
+
+print $root->minsum . "\n";