aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-038/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-038/jaldhar-h-vyas/perl/ch-1.pl42
-rwxr-xr-xchallenge-038/jaldhar-h-vyas/perl/ch-2.pl117
-rwxr-xr-xchallenge-038/jaldhar-h-vyas/raku/ch-1.p656
-rwxr-xr-xchallenge-038/jaldhar-h-vyas/raku/ch-2.p651
5 files changed, 267 insertions, 0 deletions
diff --git a/challenge-038/jaldhar-h-vyas/blog.txt b/challenge-038/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..8885c6d42b
--- /dev/null
+++ b/challenge-038/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2020/02/perl_weekly_challenge_week_38.html
diff --git a/challenge-038/jaldhar-h-vyas/perl/ch-1.pl b/challenge-038/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..dd9ebd39b1
--- /dev/null
+++ b/challenge-038/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+sub isLeap {
+ my ($year) = @_;
+ return $year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0);
+}
+
+sub isValidDate {
+ my ($year, $month, $day) = @_;
+
+ if ($month == 2 && $day > (isLeap($year) ? 29 : 28)) {
+ return undef;
+ }
+
+ if ((grep { $month == $_ } ( 4, 6, 9, 11 )) && $day > 30) {
+ return undef;
+ }
+
+ return 1;
+}
+
+my $input = shift // q{};
+
+if ($input =~ m{
+ \A
+ (?<century> [12])
+ (?<year> [0-9]{2})
+ (?<month> (0[1-9]) | (1[0-2]) )
+ (?<day> (0[1-9]) | ([1-2][0-9]) | (3[01]))
+ \z
+}gmx) {
+ my $year = (($+{century} == 1) ? 20 : 19) . $+{year};
+ if (isValidDate($year, $+{month}, $+{day})) {
+ say join q{-}, ($year, $+{month}, $+{day});
+ exit;
+ }
+}
+
+say 'Invalid date.'; \ No newline at end of file
diff --git a/challenge-038/jaldhar-h-vyas/perl/ch-2.pl b/challenge-038/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..4710c7b080
--- /dev/null
+++ b/challenge-038/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,117 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+use English qw/ -no_match_vars /;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+sub pick {
+ my @list = @{$_[0]};
+ my $length = scalar @list;
+ my $n = $_[1] // 1;
+ my @picked;
+
+ while (scalar @picked != $n) {
+ my $pos = int(rand($length));
+ if (defined $list[$pos]) {
+ push @picked, $list[$pos];
+ $list[$pos] = undef;
+ }
+ }
+
+ return wantarray ? @picked : $picked[0];
+}
+
+sub makeWords {
+ my $list = '/usr/share/dict/words';
+
+ open my $wordList, '<', $list or die "$list: $!\n";
+ local $RS;
+ my $contents = <$wordList>;
+ close $wordList;
+
+ my %words =
+ map { uc $_ => $_ }
+ grep { length() < 8 && /^[[:alpha:]]+$/ }
+ split /\n/,
+ $contents;
+
+ my %dict;
+ for my $word (keys %words) {
+ push @{$dict{ join q{}, (sort split //, $word)}}, $word;
+ }
+ return %dict;
+}
+
+sub pickTiles {
+ return pick([split //,
+ 'A' x 8 . 'G' x 3 . 'I' x 5 . 'S' x 7 . 'U' x 5 . 'X' x 2 . 'Z' x 5 .
+ 'E' x 9 . 'J' x 3 . 'L' x 3 . 'R' x 3 . 'V' x 3 . 'Y' x 5 . 'F' x 3 .
+ 'D' x 3 . 'P' x 5 . 'W' x 5 . 'B' x 5 . 'N' x 4 . 'T' x 5 . 'O' x 3 .
+ 'H' x 3 . 'M' x 4 . 'C' x 4 . 'K' x 2 . 'Q' x 2], 7);
+}
+
+sub calculateScore {
+ my ($string) = @_;
+
+ my %scores = (
+ 'A' => 1, 'G' => 1, 'I' => 1, 'S' => 1, 'U' => 1, 'X' => 1, 'Z' => 1,
+ 'E' => 2, 'J' => 2, 'L' => 2, 'R' => 2, 'V' => 2, 'Y' => 2, 'F' => 3,
+ 'D' => 3, 'P' => 3, 'W' => 3, 'B' => 4, 'N' => 4, 'T' => 5, 'O' => 5,
+ 'H' => 5, 'M' => 5, 'C' => 5, 'K' => 10, 'Q' => 10 );
+
+ my $total = 0;
+ for my $c (split //, $string) {
+ $total += $scores{$c};
+ }
+
+ return $total;
+}
+
+my %dict = makeWords();
+my @draw = pickTiles();
+
+my $bestScore = 0;
+my $bestWord = q{};
+my $length = 7;
+
+while ($length) {
+ for my $tiles
+ (map { join q{}, @{$_}; } combinations([sort @draw], $length)) {
+
+ if (exists $dict{$tiles}) {
+ my $value = @{$dict{$tiles}}[0];
+ my $score = calculateScore($value);
+
+ if ($score > $bestScore ||
+ ($score == $bestScore && length $value > length $bestWord)) {
+ $bestScore = $score;
+ $bestWord = $value;
+ }
+ }
+
+ }
+ $length--;
+}
+
+say +(join q{}, @draw), " = $bestWord ($bestScore)"; \ No newline at end of file
diff --git a/challenge-038/jaldhar-h-vyas/raku/ch-1.p6 b/challenge-038/jaldhar-h-vyas/raku/ch-1.p6
new file mode 100755
index 0000000000..da7d13e856
--- /dev/null
+++ b/challenge-038/jaldhar-h-vyas/raku/ch-1.p6
@@ -0,0 +1,56 @@
+#!/usr/bin/perl6
+
+grammar Date {
+ token TOP {
+ ^ <year> <month> <day> $
+ }
+
+ token year {
+ <century> $<shortyear> = <[0 .. 9]> ** 2
+ }
+
+ token century {
+ <[ 12 ]>
+ }
+
+ token month {
+ 0 <[1 .. 9]> || 1 <[0 .. 2]>
+ }
+
+ token day {
+ 0 <[1 .. 9]> || <[1 .. 2]> <[0 .. 9]> || 3 <[0 .. 1]>
+ }
+}
+
+class DateAction {
+ method TOP($/) {
+ if self!isValidDate($/<year>.made.Int, $/<month>.Int, $/<day>.Int) {
+ make ($/<year>.made, $/<month>, $/<day>).join(q{-});
+ }
+ }
+
+ method year($/) {
+ make ($/<century> == 1 ?? 20 !! 19) ~ $/<shortyear>;
+ }
+
+ method !isLeap(Int $year) {
+ return $year %% 4 && ($year !%% 100 || $year %% 400);
+ }
+
+ method !isValidDate(Int $year, Int $month, Int $day) {
+ if $month == 2 && $day > (self!isLeap($year) ?? 29 !! 28) {
+ return False;
+ }
+
+ if ($month == any ( 4, 6, 9, 11 )) && $day > 30 {
+ return False;
+ }
+
+ return True;
+ }
+}
+
+multi sub MAIN(Str $input) {
+ my $date = Date.parse($input, actions => DateAction.new);
+ say $date && $date.made ?? $date.made !! 'Invalid date.';
+}
diff --git a/challenge-038/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-038/jaldhar-h-vyas/raku/ch-2.p6
new file mode 100755
index 0000000000..303137621f
--- /dev/null
+++ b/challenge-038/jaldhar-h-vyas/raku/ch-2.p6
@@ -0,0 +1,51 @@
+#!/usr/bin/perl6
+
+sub makeWords() {
+ return '/usr/share/dict/words'.IO.lines
+ .grep({ .chars < 8 && / ^ <:alpha>+ $ / })
+ .classify({ $_.comb.sort.join.uc; }, :as{ uc $_; })
+ .sort;
+}
+
+sub pickTiles() {
+ return
+ ('A' x 8 ~ 'G' x 3 ~ 'I' x 5 ~ 'S' x 7 ~ 'U' x 5 ~ 'X' x 2 ~ 'Z' x 5 ~
+ 'E' x 9 ~ 'J' x 3 ~ 'L' x 3 ~ 'R' x 3 ~ 'V' x 3 ~ 'Y' x 5 ~ 'F' x 3 ~
+ 'D' x 3 ~ 'P' x 5 ~ 'W' x 5 ~ 'B' x 5 ~ 'N' x 4 ~ 'T' x 5 ~ 'O' x 3 ~
+ 'H' x 3 ~ 'M' x 4 ~ 'C' x 4 ~ 'K' x 2 ~ 'Q' x 2).comb.pick(7);
+}
+
+sub calculateScore(Str $string) {
+ my %scores = (
+ 'A' => 1, 'G' => 1, 'I' => 1, 'S' => 1, 'U' => 1, 'X' => 1, 'Z' => 1,
+ 'E' => 2, 'J' => 2, 'L' => 2, 'R' => 2, 'V' => 2, 'Y' => 2, 'F' => 3,
+ 'D' => 3, 'P' => 3, 'W' => 3, 'B' => 4, 'N' => 4, 'T' => 5, 'O' => 5,
+ 'H' => 5, 'M' => 5, 'C' => 5, 'K' => 10, 'Q' => 10 );
+
+ return [+] $string.comb.map({ %scores{$_}; });
+}
+
+multi sub MAIN() {
+ my %dict = makeWords();
+ my @draw = pickTiles();
+
+ my $bestScore = 0;
+ my $bestWord = q{};
+
+ for (7 ... 1) -> $length {
+ for @draw.sort.combinations($length).map({ .join }) -> $tiles {
+ if %dict{$tiles} {
+ my $value = %dict{$tiles}.values[0];
+ my $score = calculateScore($value);
+
+ if ($score > $bestScore ||
+ ($score == $bestScore && $value.chars > $bestWord.chars)) {
+ $bestScore = $score;
+ $bestWord = $value;
+ }
+ }
+ }
+ }
+
+ say @draw.join, " = $bestWord ($bestScore)";
+} \ No newline at end of file