From 271a7951453e9f875baf518cb406cda088add12c Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Sun, 14 May 2023 23:42:02 -0400 Subject: Challenge 216 by Jaldhar H. Vyas. --- challenge-216/jaldhar-h-vyas/blog.txt | 1 + challenge-216/jaldhar-h-vyas/perl/ch-1.pl | 49 ++++++++++++++++ challenge-216/jaldhar-h-vyas/perl/ch-2.pl | 89 +++++++++++++++++++++++++++++ challenge-216/jaldhar-h-vyas/raku/ch-1.raku | 17 ++++++ challenge-216/jaldhar-h-vyas/raku/ch-2.raku | 26 +++++++++ 5 files changed, 182 insertions(+) create mode 100644 challenge-216/jaldhar-h-vyas/blog.txt create mode 100755 challenge-216/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-216/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-216/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-216/jaldhar-h-vyas/raku/ch-2.raku diff --git a/challenge-216/jaldhar-h-vyas/blog.txt b/challenge-216/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..5cfbd875fe --- /dev/null +++ b/challenge-216/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/05/perl_weekly_challenge_week_216.html diff --git a/challenge-216/jaldhar-h-vyas/perl/ch-1.pl b/challenge-216/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..c7769971e3 --- /dev/null +++ b/challenge-216/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub isSubset { + my @subset = @{$_[0]}; + + my %set; + for my $c (@{$_[1]}) { + $set{$c}++; + } + + for my $c (@subset) { + if (!exists $set{$c} || $set{$c} == 0) { + return undef; + } + $set{$c}--; + } + + return 1; +} + + +sub combGrepUniqueLc { + my ($str) = @_; + + my @chars = map { lc } grep { $_ =~ /[[:alpha:]]/ } split //, $str; + + my %unique; + for my $c (@chars) { + $unique{$c}++; + } + + return keys %unique; +} + +my ($reg, @words) = @ARGV; + +my @registration = combGrepUniqueLc($reg); +my @results; + +for my $word (@words) { + my @w = combGrepUniqueLc($word); + if (isSubset(\@registration, \@w)) { + push @results, $word; + } +} + +say q{(}, (join q{, }, map {"'$_'"} @results), q{)}; diff --git a/challenge-216/jaldhar-h-vyas/perl/ch-2.pl b/challenge-216/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..368e974b8c --- /dev/null +++ b/challenge-216/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +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 combUnique { + my ($str) = @_; + + my @chars = split //, $str; + + my %unique; + for my $c (@chars) { + $unique{$c}++; + } + + return keys %unique; +} + +sub isSubset { + my @subset = @{$_[0]}; + + my %set; + for my $c (@{$_[1]}) { + $set{$c}++; + } + + for my $c (@subset) { + if (!exists $set{$c} || $set{$c} == 0) { + return undef; + } + $set{$c}--; + } + + return 1; +} + +sub xx { + my ($array, $amount) = @_; + my @result = @{$array}; + + for (1 .. $amount - 1) { + push @result, @{$array}; + } + + return @result; +} + +my ($word, @stickers) = @ARGV; + +unless (isSubset([combUnique($word)], [combUnique(join q{}, @stickers)])) { + say 0; + exit; +} + +my @stickerList = xx(\@stickers, scalar combUnique($word)); +my @chars = split //, $word; +my $result = 0; + +LOOP: for my $i (1 .. scalar @stickerList) { + for my $combo (combinations(\@stickerList, $i)) { + my @sticker = split //, (join q{}, @{$combo}); + if (isSubset(\@chars, \@sticker)) { + $result = scalar @{$combo}; + last LOOP; + } + } +} + +say $result; diff --git a/challenge-216/jaldhar-h-vyas/raku/ch-1.raku b/challenge-216/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..610fcff0ad --- /dev/null +++ b/challenge-216/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!/usr/bin/raku + +sub MAIN( + $reg, *@words +) { + my @registration = $reg.comb.grep({$_ ~~ //}).unique.map({ $_.lc }); + my @results; + + for @words -> $word { + my @w = $word.comb.grep({$_ ~~ //}).unique.map({ $_.lc }); + if @registration ⊆ @w { + @results.push($word); + } + } + + say q{(}, @results.map({"'$_'"}).join(q{, }), q{)}; +} \ No newline at end of file diff --git a/challenge-216/jaldhar-h-vyas/raku/ch-2.raku b/challenge-216/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..6d314a1bca --- /dev/null +++ b/challenge-216/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,26 @@ +#!/usr/bin/raku + +sub MAIN( + $word, *@stickers +) { + + if $word.comb.unique ⊈ @stickers.join.comb.unique { + say 0; + exit; + } + my @stickerList = (@stickers xx $word.comb.unique.elems).flat; + my $chars = Bag.new($word.comb); + my $result = 0; + + LOOP: for 1 .. @stickerList.elems -> $i { + for @stickerList.combinations($i) -> @combo { + my $sticker = Bag.new(@combo.join.comb); + if $chars ⊆ $sticker { + $result = @combo.elems; + last LOOP; + } + } + } + + say $result; +} \ No newline at end of file -- cgit