diff options
| author | Flavio Poletti <flavio@polettix.it> | 2023-02-04 19:57:59 +0100 |
|---|---|---|
| committer | Flavio Poletti <flavio@polettix.it> | 2023-02-04 19:57:59 +0100 |
| commit | f8f84afa479607cb91778a2f60c4e10920884c31 (patch) | |
| tree | 1b4d0398d85e3e3be1f20e3ef0549e53759294d6 /challenge-202 | |
| parent | 1b0c04f4b45857a5b8024860663bf58f353bf1ed (diff) | |
| download | perlweeklychallenge-club-f8f84afa479607cb91778a2f60c4e10920884c31.tar.gz perlweeklychallenge-club-f8f84afa479607cb91778a2f60c4e10920884c31.tar.bz2 perlweeklychallenge-club-f8f84afa479607cb91778a2f60c4e10920884c31.zip | |
Add polettix's solution to challenge-202
Diffstat (limited to 'challenge-202')
| -rw-r--r-- | challenge-202/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-202/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-202/polettix/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-202/polettix/perl/ch-2.pl | 49 | ||||
| -rw-r--r-- | challenge-202/polettix/raku/ch-1.raku | 31 | ||||
| -rw-r--r-- | challenge-202/polettix/raku/ch-2.raku | 47 |
6 files changed, 162 insertions, 0 deletions
diff --git a/challenge-202/polettix/blog.txt b/challenge-202/polettix/blog.txt new file mode 100644 index 0000000000..09838de191 --- /dev/null +++ b/challenge-202/polettix/blog.txt @@ -0,0 +1 @@ +https://etoobusy.polettix.it/2023/02/02/pwc202-consecutive-odds/ diff --git a/challenge-202/polettix/blog1.txt b/challenge-202/polettix/blog1.txt new file mode 100644 index 0000000000..2039f2b51d --- /dev/null +++ b/challenge-202/polettix/blog1.txt @@ -0,0 +1 @@ +https://etoobusy.polettix.it/2023/02/03/pwc202-widest-valley/ diff --git a/challenge-202/polettix/perl/ch-1.pl b/challenge-202/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..c0a97fb1dd --- /dev/null +++ b/challenge-202/polettix/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +my ($n_streaks, $longest_streak) = consecutive_odds(@ARGV); + +# strict +say {*STDERR} '(one single streak of exactly three odds, no other odd)'; +say {*STDOUT} $n_streaks == 1 && $longest_streak == 3 ? 1 : 0; + +# lax +say {*STDERR} 'at least three odds in a row'; +say {*STDOUT} $longest_streak >= 3 ? 1 : 0; + +sub consecutive_odds (@array) { + my $longest_streak = 0; + my $current_streak = 0; + my $n_streaks = 0; + for my $item (@array) { + if ($item % 2) { + ++$current_streak; + ++$longest_streak if $longest_streak < $current_streak; + } + else { + ++$n_streaks if $current_streak; + $current_streak = 0; + } + } + ++$n_streaks if $current_streak; + return ($n_streaks, $longest_streak); +} diff --git a/challenge-202/polettix/perl/ch-2.pl b/challenge-202/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..22d26f76a3 --- /dev/null +++ b/challenge-202/polettix/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +my @valley = + widest_valley(grep { defined } map { split m{\D+}mxs, } @ARGV); +say join ', ', @valley; + +sub widest_valley (@altitudes) { + return @altitudes if @altitudes < 2; # trivial cases + + my $db = 0; # start of a valley + my $lb = 0; # start of a level + my $going_up = 0; # start going down + my ($vb, $vl) = (0, 1); # best valley so far + + my $previous = $altitudes[0]; + for my $i (1 .. $#altitudes) { + my $current = $altitudes[$i]; + + if ($previous < $current) { # going up + $lb = $i; # reset the level begin + $going_up = 1; # record the direction + } + + # do nothing if $previous == $current + + elsif ($previous > $current) { # going down + if ($going_up) { # leaving the top, "close" a valley + my $length = $i - $db; + ($vb, $vl) = ($db, $length) if $length > $vl; + + $db = $lb; # record the start of the new valley + $going_up = 0; # record the direction + } ## end if ($going_up) + $lb = $i; # reset the level begin + } ## end elsif ($previous > $current) + + $previous = $current; # prepare for the next iteration + } ## end for my $i (1 .. $#altitudes) + + # anyway, close the last segment + my $length = @altitudes - $db; + ($vb, $vl) = ($db, $length) if $length > $vl; + + return @altitudes[$vb .. ($vb + $vl - 1)]; +} ## end sub widest_valley diff --git a/challenge-202/polettix/raku/ch-1.raku b/challenge-202/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..ea49384754 --- /dev/null +++ b/challenge-202/polettix/raku/ch-1.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (*@args) { + my ($n-streaks, $longest-streak) = consecutive-odds(@args); + + # strict + $*ERR.put('(one single streak of exactly three odds, no other odd)'); + $*OUT.put(($n-streaks == 1 && $longest-streak == 3) ?? 1 !! 0); + + # lax + $*ERR.put('at least three odds in a row'); + $*OUT.put(($longest-streak >= 3) ?? 1 !! 0); +} + +sub consecutive-odds (@array) { + my $longest-streak = 0; + my $current-streak = 0; + my $n-streaks = 0; + for @array -> $item { + if $item %% 2 { + ++$n-streaks if $current-streak; + $current-streak = 0; + } + else { + ++$current-streak; + ++$longest-streak if $longest-streak < $current-streak; + } + } + ++$n-streaks if $current-streak; + return $n-streaks, $longest-streak; +} diff --git a/challenge-202/polettix/raku/ch-2.raku b/challenge-202/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..2e8bf02415 --- /dev/null +++ b/challenge-202/polettix/raku/ch-2.raku @@ -0,0 +1,47 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (*@args) { + my @valley = widest-valley([@args.map({.comb(/\d+/)}).flatĀ».Int]); + put @valley.join(', '); +} + + +sub widest-valley (@altitudes) { + return @altitudes if @altitudes < 2; # trivial cases + + my $db = 0; # start of a valley + my $lb = 0; # start of a level + my $going_up = 0; # start going down + my ($vb, $vl) = 0, 1; # best valley so far + + my $previous = @altitudes[0]; + for 1 ..^ @altitudes -> $i { + my $current = @altitudes[$i]; + + if $previous < $current { # going up + $lb = $i; # reset the level begin + $going_up = 1; # record the direction + } + + # do nothing if $previous == $current + + elsif $previous > $current { # going down + if ($going_up) { # leaving the top, "close" a valley + my $length = $i - $db; + ($vb, $vl) = $db, $length if $length > $vl; + + $db = $lb; # record the start of the new valley + $going_up = 0; # record the direction + } ## end if ($going_up) + $lb = $i; # reset the level begin + } ## end elsif ($previous > $current) + + $previous = $current; # prepare for the next iteration + } ## end for my $i (1 .. $#altitudes) + + # anyway, close the last segment + my $length = @altitudes - $db; + ($vb, $vl) = $db, $length if $length > $vl; + + return @altitudes[$vb .. ($vb + $vl - 1)]; +} ## end sub widest_valley |
