From a543d8cc9724f838b37f4dd45210114364b207c0 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 25 Jun 2019 00:10:35 +0200 Subject: Add solution to 014 by E. Choroba --- challenge-014/e-choroba/perl5/ch-1.pl | 27 +++++++++++++++++++++++++++ challenge-014/e-choroba/perl5/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100755 challenge-014/e-choroba/perl5/ch-1.pl create mode 100755 challenge-014/e-choroba/perl5/ch-2.pl diff --git a/challenge-014/e-choroba/perl5/ch-1.pl b/challenge-014/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..b47e1705f0 --- /dev/null +++ b/challenge-014/e-choroba/perl5/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +sub van_eck { + my %latest; + my ($n, $a_n) = (0, 0); + return sub { + my $r = $a_n; + my $a_n_plus_1 = exists $latest{$a_n} + ? $n - $latest{$a_n} + : 0; + $latest{$a_n} = $n++; + $a_n = $a_n_plus_1; + return $r + } +} + +use Test::More; + +my $iterator = van_eck(); + +is_deeply([map $iterator->(), 1 .. 19], + [0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5]); + +done_testing(); diff --git a/challenge-014/e-choroba/perl5/ch-2.pl b/challenge-014/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..ca5a4dc3e8 --- /dev/null +++ b/challenge-014/e-choroba/perl5/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +my %states; +@states{qw(AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME + MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI + SC SD TN TX UT VT VA WA WV WI WY)} = (); + +my $dictionary = '/usr/share/dict/british'; +open my $in, '<', $dictionary or die $!; +my @longest = (""); +while (my $word = <$in>) { + chomp $word; + next if 1 & length $word; + + my $uc_word = uc $word; + my @pairs = $uc_word =~ /\G(..)/g; + + next if length $uc_word != 2 * @pairs; + + next if grep ! exists $states{$_}, @pairs; + + next if length($word) < length $longest[0]; + + if (length($word) == length $longest[0]) { + push @longest, $word; + } else { + @longest = ($word); + } +} +say for @longest; -- cgit