aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2022-12-05 18:12:15 -0500
committerDave Jacoby <jacoby.david@gmail.com>2022-12-05 18:12:15 -0500
commit4573b40d38909f12fb344418b2e8c767d32a77a0 (patch)
tree2a143ff7e05a7696cbc1dcdba9241e8569bf5ce6
parent8d4ad39acceae6916068d7661648e075877837cc (diff)
downloadperlweeklychallenge-club-4573b40d38909f12fb344418b2e8c767d32a77a0.tar.gz
perlweeklychallenge-club-4573b40d38909f12fb344418b2e8c767d32a77a0.tar.bz2
perlweeklychallenge-club-4573b40d38909f12fb344418b2e8c767d32a77a0.zip
Challege 194 Done Earlier Than Normal1
-rw-r--r--challenge-194/dave-jacoby/perl/ch-1.pl35
-rw-r--r--challenge-194/dave-jacoby/perl/ch-2.pl42
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-194/dave-jacoby/perl/ch-1.pl b/challenge-194/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..366233e0a8
--- /dev/null
+++ b/challenge-194/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc say postderef signatures state };
+
+my @times = qw{
+ ?5:00
+ ?3:00
+ 1?:00
+ 2?:00
+ 12:?5
+ 12:5?
+};
+
+for my $time (@times) {
+ my $output = digital_clock($time);
+ say <<"END";
+ Input: \$time = '$time'
+ Output: $output
+END
+}
+
+sub digital_clock ( $input ) {
+ my $valid = '';
+ for my $i ( 0 .. 9 ) {
+ my $j = $input;
+ $j =~ s/\?/$i/mx;
+ my ( $h, $m ) = split m{:}, $j;
+ return $valid if $h > 23;
+ return $valid if $m > 59;
+ $valid = $i;
+ }
+ return $valid;
+}
diff --git a/challenge-194/dave-jacoby/perl/ch-2.pl b/challenge-194/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..5e59084c61
--- /dev/null
+++ b/challenge-194/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+my @examples = qw{ abbc xyzyyxz xzxz };
+for my $e (@examples) {
+ my $o = analyze($e);
+ say <<"END";
+ Input: \$s = '$e'
+ Output: $o
+END
+}
+
+sub analyze( $string ) {
+ my $count = count_chars($string);
+ my @values = values $count->%*;
+
+ for my $i ( 0 .. -1 + scalar @values ) {
+ my $j = $values[$i];
+ my @array = @values;
+ $array[$i] = undef;
+ @array = grep { defined } @array;
+ my $c = 0;
+ my $d = scalar @array;
+ for my $v ( @array ) {
+ $c++ if $v + 1 == $j;
+ }
+ return 1 if $c == $d;
+ }
+
+ return 0;
+}
+
+sub count_chars ( $string ) {
+ my $output;
+ for my $i ( 0 .. -1 + length $string ) {
+ $output->{ substr $string, $i, 1 }++;
+ }
+ return $output;
+}