aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-076/nunovieira220/perl/ch-1.pl31
-rw-r--r--challenge-076/nunovieira220/perl/ch-2.pl116
-rw-r--r--challenge-076/nunovieira220/perl/dict.txt53
-rw-r--r--challenge-076/nunovieira220/perl/grid.txt19
4 files changed, 219 insertions, 0 deletions
diff --git a/challenge-076/nunovieira220/perl/ch-1.pl b/challenge-076/nunovieira220/perl/ch-1.pl
new file mode 100644
index 0000000000..c0f895f75d
--- /dev/null
+++ b/challenge-076/nunovieira220/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use List::Util qw[min];
+use Math::Prime::Util qw[is_prime];
+
+# Calculate prime num
+sub prime_sum {
+ my $N = $_[0];
+ my %calcs = ();
+ $calcs{2} = 1;
+
+ for(my $i = 3; $i <= $N; $i += 2) {
+ if(is_prime($i)) {
+ for my $key (keys %calcs) {
+ my $sum = $i + $key;
+ my $inc = $calcs{$key} + 1;
+ $calcs{$sum} = $calcs{$sum} ? min($calcs{$sum}, $inc) : $inc;
+ }
+
+ $calcs{$i} = 1;
+ }
+ }
+
+ return $calcs{$N} || 0;
+}
+
+# Input/Output
+my $N = scalar @ARGV ? $ARGV[0] : 9;
+print prime_sum($N)."\n"; \ No newline at end of file
diff --git a/challenge-076/nunovieira220/perl/ch-2.pl b/challenge-076/nunovieira220/perl/ch-2.pl
new file mode 100644
index 0000000000..de5889aad4
--- /dev/null
+++ b/challenge-076/nunovieira220/perl/ch-2.pl
@@ -0,0 +1,116 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+sub get_word {
+ my $build = $_[0];
+ my $val = $_[1];
+ my %dict = %{$_[2]};
+ my @res = ();
+
+ if (grep(/^$build$/, @{$dict{$val}})) {
+ my $index = 0;
+ $index++ until $dict{$val}[$index] eq $build;
+ splice(@{$dict{$val}}, $index, 1);
+ push @res, $build;
+ }
+
+ return @res;
+}
+
+# Search grid
+sub search_grid {
+ my @grid = @{$_[0]};
+ my %dict = %{$_[1]};
+ my %revs = %{$_[2]};
+ my $gridX = scalar @grid;
+ my $gridY = scalar @{$grid[0]};
+ my @res = ();
+
+ for(my $i = 0; $i < $gridX; $i++) {
+ for(my $j = 0; $j < $gridY; $j++) {
+ my $val = $grid[$i][$j];
+ next if(!$dict{$val});
+
+ #horizontal
+ my $build = $val;
+
+ for(my $k = $j + 1; $k < $gridY; $k++) {
+ $build .= $grid[$i][$k];
+ push @res, get_word($build, $val, \%dict);
+ last if (!grep(/$build/, @{$dict{$val}}));
+ }
+
+ #vertical
+ $build = $val;
+
+ for(my $k = $i + 1; $k < $gridX; $k++) {
+ $build .= $grid[$k][$j];
+ push @res, get_word($build, $val, \%dict);
+ last if (!grep(/$build/, @{$dict{$val}}));
+ }
+
+ #diagonal down
+ $build = $val;
+ my $k1 = $i + 1;
+ my $k2 = $j + 1;
+
+ while($k1 < $gridX && $k2 < $gridY) {
+ $build .= $grid[$k1][$k2];
+ push @res, get_word($build, $val, \%dict);
+ last if (!grep(/$build/, @{$dict{$val}}));
+ $k1++; $k2++;
+ }
+
+ #diagonal up
+ $build = $val;
+ $k1 = $i + 1;
+ $k2 = $j - 1;
+
+ while($k1 < $gridX && $k2 > 0) {
+ $build .= $grid[$k1][$k2];
+ push @res, get_word($build, $val, \%dict);
+ last if (!grep(/$build/, @{$dict{$val}}));
+ $k1++; $k2--;
+ }
+ }
+ }
+
+ # Print words
+ my %final = map { $revs{$_} ? ($revs{$_} => 1) : ($_ => 1) } @res;
+ print $_."\n" for (sort keys %final);
+}
+
+# Input/Output
+open(my $fh1, "<", "grid.txt") or die "Can't open < grid.txt: $!";
+open(my $fh2, "<", "dict.txt") or die "Can't open < dict.txt: $!";
+
+my @grid = ();
+while(<$fh1>) {
+ chomp;
+ my @arr = split(/ /, lc($_));
+ push @grid, \@arr;
+}
+
+my %dict = ();
+my %revs = ();
+while(<$fh2>) {
+ chomp;
+ my $line = $_;
+ my $rev = reverse($line);
+
+ my $lineFirst = substr($line, 0, 1);
+ $dict{$lineFirst} = () if(!$dict{$lineFirst});
+ push @{$dict{$lineFirst}}, lc($line);
+
+ my $revFirst = substr($rev, 0, 1);
+ $dict{$revFirst} = () if(!$dict{$revFirst});
+ push @{$dict{$revFirst}}, lc($rev);
+ $revs{$rev} = $line;
+}
+
+search_grid(\@grid, \%dict, \%revs);
+
+close($fh1);
+close($fh2); \ No newline at end of file
diff --git a/challenge-076/nunovieira220/perl/dict.txt b/challenge-076/nunovieira220/perl/dict.txt
new file mode 100644
index 0000000000..bbb2714ce7
--- /dev/null
+++ b/challenge-076/nunovieira220/perl/dict.txt
@@ -0,0 +1,53 @@
+argos
+margo
+patna
+traci
+tracie
+aimed
+align
+antes
+arose
+ashed
+blunt
+blunts
+broad
+buries
+clove
+cloven
+constitution
+constitutions
+croon
+depart
+departed
+enter
+filch
+garlic
+goats
+grieve
+grieves
+hazard
+liens
+malign
+malignant
+malls
+midst
+ought
+ovary
+parted
+pudgiest
+quash
+quashed
+ruses
+shrine
+shrines
+social
+socializing
+spasm
+spasmodic
+succor
+succors
+theorem
+theorems
+virus
+viruses
+wigged \ No newline at end of file
diff --git a/challenge-076/nunovieira220/perl/grid.txt b/challenge-076/nunovieira220/perl/grid.txt
new file mode 100644
index 0000000000..c5766eae93
--- /dev/null
+++ b/challenge-076/nunovieira220/perl/grid.txt
@@ -0,0 +1,19 @@
+B I D E M I A T S U C C O R S T
+L D E G G I W Q H O D E E H D P
+U S E I R U B U T E A S L A G U
+N G N I Z I L A I C O S C N U D
+T G M I D S T S A R A R E I F G
+S R E N M D C H A S I V E E L I
+S C S H A E U E B R O A D M T E
+H W O V L P E D D L A I U L S S
+R Y O N L A S F C S T A O G O T
+I G U S S R R U G O V A R Y O C
+N R G P A T N A N G I L A M O O
+E I H A C E I V I R U S E S E D
+S E T S U D T T G A R L I C N H
+H V R M X L W I U M S N S O T B
+A E A O F I L C H T O D C A E U
+Z S C D F E C A A I I R L N R F
+A R I I A N Y U T O O O U T P F
+R S E C I S N A B O S C N E R A
+D R S M P C U U N E L T E S I L \ No newline at end of file