From 4573b40d38909f12fb344418b2e8c767d32a77a0 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 5 Dec 2022 18:12:15 -0500 Subject: Challege 194 Done Earlier Than Normal1 --- challenge-194/dave-jacoby/perl/ch-1.pl | 35 ++++++++++++++++++++++++++++ challenge-194/dave-jacoby/perl/ch-2.pl | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 challenge-194/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-194/dave-jacoby/perl/ch-2.pl 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; +} -- cgit