commit 70b4a705dcf606e65388de33cc16d97fd4539f33
parent 317e683ef35290d8fe4c829fc6d78179277ca992
Author: Ryan Wolf <rwolf@borderstylo.com>
Date: Sun, 17 Oct 2010 14:05:36 -0700
soln to level one of greplin
Diffstat:
1 file changed, 34 insertions(+), 0 deletions(-)
diff --git a/greplin/level1.pl b/greplin/level1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+open(my $file, 'level1.txt');
+my $text = <$file>;
+close($file);
+
+chomp($text);
+
+# generate list of all substrings
+my %strings;
+
+sub gen_substrings {
+ my $string = shift;
+ if (defined($strings{$string})) {
+ return ();
+ }
+ $strings{$string} = 1;
+ my $l = length($string);
+ if ($l == 3) { return ($string); }
+
+ my $first = substr($string, 0, $l - 1);
+ my $last = substr($string, 1);
+ return ($string, gen_substrings($first), gen_substrings($last));
+}
+
+my @substrings = gen_substrings($text);
+
+# filter palindrones
+my @palindrones = grep { $_ eq reverse($_) } @substrings;
+
+# sort by length, take first
+my @sorted = sort { length($b) <=> length($a) } @palindrones;
+
+print shift(@sorted) . "\n";