From a60e110825aedc3295dfd1e033e2f2c6441d03c2 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Tue, 26 Aug 2025 18:39:05 -0400 Subject: Challenge 336 by Jaldhar H. Vyas. --- challenge-336/jaldhar-h-vyas/blog.txt | 1 + challenge-336/jaldhar-h-vyas/perl/ch-1.pl | 25 ++++++++++++++++++++++ challenge-336/jaldhar-h-vyas/perl/ch-2.pl | 32 +++++++++++++++++++++++++++++ challenge-336/jaldhar-h-vyas/raku/ch-1.sh | 3 +++ challenge-336/jaldhar-h-vyas/raku/ch-2.raku | 27 ++++++++++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 challenge-336/jaldhar-h-vyas/blog.txt create mode 100755 challenge-336/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-336/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-336/jaldhar-h-vyas/raku/ch-1.sh create mode 100755 challenge-336/jaldhar-h-vyas/raku/ch-2.raku diff --git a/challenge-336/jaldhar-h-vyas/blog.txt b/challenge-336/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..5820849dc5 --- /dev/null +++ b/challenge-336/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2025/08/perl_weekly_challenge_week_336.html diff --git a/challenge-336/jaldhar-h-vyas/perl/ch-1.pl b/challenge-336/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..ee2d255b22 --- /dev/null +++ b/challenge-336/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +use 5.038; +use warnings; + +sub gcd($a, $b) { + if ($b == 0) { + return $a; + } + + gcd($b, $a % $b); +} + +my @ints = @ARGV; +my %c; + +for my $int (@ints) { + $c{$int}++; +} + +my $gcd = 0; +for my $v (values %c) { + $gcd = gcd($gcd, $v); +} + +say $gcd >= 2 ? 'true' : 'false'; diff --git a/challenge-336/jaldhar-h-vyas/perl/ch-2.pl b/challenge-336/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..61d30b88fa --- /dev/null +++ b/challenge-336/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +use 5.038; +use warnings; + +sub sum(@elems) { + my $total; + for my $n (@elems) { + $total += $n; + } + + return $total; +} + +my @scores = @ARGV; +my @previous; + +for my $score (@scores) { + if ($score eq 'C') { + pop @previous; + } + elsif ($score eq 'D') { + push @previous, 2 * $previous[-1]; + } + elsif ($score eq '+') { + push @previous, $previous[-1] + $previous[-2]; + } + else { + push @previous, $score; + } +} + +say sum(@previous); diff --git a/challenge-336/jaldhar-h-vyas/raku/ch-1.sh b/challenge-336/jaldhar-h-vyas/raku/ch-1.sh new file mode 100755 index 0000000000..8078b5a6c8 --- /dev/null +++ b/challenge-336/jaldhar-h-vyas/raku/ch-1.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +raku -e '@*ARGS.classify({$_},:into(my %c));say ([gcd] %c.values)>=2' "$@" \ No newline at end of file diff --git a/challenge-336/jaldhar-h-vyas/raku/ch-2.raku b/challenge-336/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..a1594dc163 --- /dev/null +++ b/challenge-336/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/raku + +sub MAIN( + *@scores +) { + my @previous; + + for @scores -> $score { + given $score { + when 'C' { + @previous.pop; + } + when 'D' { + @previous.push(2 * @previous[*-1]); + } + when '+' { + @previous.push(@previous[*-1] + @previous[*-2]); + + } + default { + @previous.push($score); + } + } + } + + say @previous.sum; +} \ No newline at end of file -- cgit