aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlavio Poletti <flavio@polettix.it>2022-10-20 23:41:50 +0200
committerFlavio Poletti <flavio@polettix.it>2022-10-20 23:41:50 +0200
commit88b303974e727acdc69d264c5b10d9e66fdcf7aa (patch)
tree142972b22b4ab558ddf97b31f3cc576705ab8719
parentc9d4aa109f05ded505b5d9bbb318809e924acc04 (diff)
downloadperlweeklychallenge-club-88b303974e727acdc69d264c5b10d9e66fdcf7aa.tar.gz
perlweeklychallenge-club-88b303974e727acdc69d264c5b10d9e66fdcf7aa.tar.bz2
perlweeklychallenge-club-88b303974e727acdc69d264c5b10d9e66fdcf7aa.zip
Add polettix's solution to challenge-187
-rw-r--r--challenge-187/polettix/blog.txt1
-rw-r--r--challenge-187/polettix/blog1.txt1
-rw-r--r--challenge-187/polettix/perl/ch-1.pl27
-rw-r--r--challenge-187/polettix/perl/ch-2.pl56
-rw-r--r--challenge-187/polettix/raku/ch-1.raku24
-rw-r--r--challenge-187/polettix/raku/ch-2.raku23
6 files changed, 132 insertions, 0 deletions
diff --git a/challenge-187/polettix/blog.txt b/challenge-187/polettix/blog.txt
new file mode 100644
index 0000000000..ae1e346930
--- /dev/null
+++ b/challenge-187/polettix/blog.txt
@@ -0,0 +1 @@
+https://etoobusy.polettix.it/2022/10/20/pwc187-days-together/
diff --git a/challenge-187/polettix/blog1.txt b/challenge-187/polettix/blog1.txt
new file mode 100644
index 0000000000..dfae7b53c1
--- /dev/null
+++ b/challenge-187/polettix/blog1.txt
@@ -0,0 +1 @@
+https://etoobusy.polettix.it/2022/10/21/pwc187-magical-triplets/
diff --git a/challenge-187/polettix/perl/ch-1.pl b/challenge-187/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..f20eb70bb1
--- /dev/null
+++ b/challenge-187/polettix/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+use List::Util qw< min max >;
+
+my $foo = [ split m{\s+}mxs, shift // '12-01 20-01' ];
+my $bar = [ split m{\s+}mxs, shift // '15-01 18-01' ];
+say days_together($foo, $bar);
+
+sub days_together ($foo, $bar) {
+ my $start = max(map { date_to_index($_->[0]) } ($foo, $bar));
+ my $stop = min(map { date_to_index($_->[1]) } ($foo, $bar));
+ return max(0, $stop - $start + 1);
+}
+
+sub date_to_index ($date) {
+ state $days_upto = [ days_upto() ];
+ my ($d, $m) = map { $_ + 0 } split m{-}mxs, $date;
+ return $days_upto->[$m - 1] + $d;
+}
+
+sub days_upto {
+ my $sum = 0;
+ map { $sum += $_ } qw< 0 31 28 31 30 31 30 31 31 30 31 30 31 >;
+}
diff --git a/challenge-187/polettix/perl/ch-2.pl b/challenge-187/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..f9b08c98eb
--- /dev/null
+++ b/challenge-187/polettix/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+
+my @result = magical_triplets(@ARGV);
+say '(', join(', ', @result), ')';
+
+sub magical_triplets (@n) {
+ my $it = combinations_iterator(3, @n);
+ my ($best, $best_score);
+ while (my ($combination, $complement) = $it->()) {
+ my $score = is_triangle($combination->@*) or next;
+ ($best, $best_score) = ($combination, $score)
+ if (! defined $best) || ($best_score < $score);
+ }
+ return reverse sort {$a <=> $b} ($best // [])->@*;
+}
+
+sub is_triangle ($A, $B, $C) {
+ return 0 if
+ $A >= $B + $C
+ || $B >= $C + $A
+ || $C >= $A + $B;
+ return $A + $B + $C;
+}
+
+sub combinations_iterator ($k, @items) {
+ my @indexes = (0 .. ($k - 1));
+ my $n = @items;
+ return sub {
+ return unless @indexes;
+ my (@combination, @remaining);
+ my $j = 0;
+ for my $i (0 .. ($n - 1)) {
+ if ($j < $k && $i == $indexes[$j]) {
+ push @combination, $items[$i];
+ ++$j;
+ }
+ else {
+ push @remaining, $items[$i];
+ }
+ }
+ for my $incc (reverse(-1, 0 .. ($k - 1))) {
+ if ($incc < 0) {
+ @indexes = (); # finished!
+ }
+ elsif ((my $v = $indexes[$incc]) < $incc - $k + $n) {
+ $indexes[$_] = ++$v for $incc .. ($k - 1);
+ last;
+ }
+ }
+ return (\@combination, \@remaining);
+ }
+}
diff --git a/challenge-187/polettix/raku/ch-1.raku b/challenge-187/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..4f087a9fd7
--- /dev/null
+++ b/challenge-187/polettix/raku/ch-1.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN ($foo = '12-01 20-01', $bar = '15-01 18-01') {
+ my @foo = $foo.split(/\s+/);
+ my @bar = $bar.split(/\s+/);
+ put days-together(@foo, @bar);
+}
+
+sub days-together (@foo, @bar) {
+ my $start = (@foo[0], @bar[0]).map({date-to-index($_)}).max;
+ my $stop = (@foo[1], @bar[1]).map({date-to-index($_)}).min;
+ return (0, $stop - $start + 1).max;
+}
+
+sub date-to-index ($date) {
+ state @days-upto = days-upto();
+ my ($d, $m) = $date.split(/\-/).map: * + 0;
+ return @days-upto[$m - 1] + $d;
+}
+
+sub days-upto {
+ my $sum = 0;
+ <0 31 28 31 30 31 30 31 31 30 31 30 31>.map: $sum += *;
+}
diff --git a/challenge-187/polettix/raku/ch-2.raku b/challenge-187/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..13fbd366c3
--- /dev/null
+++ b/challenge-187/polettix/raku/ch-2.raku
@@ -0,0 +1,23 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN (*@args) { put magical-triplets(@args) }
+
+sub magical-triplets (@n) {
+ my ($best, $best-score);
+ for @n.combinations(3) -> $comb {
+ my $score = is-triangle($comb) or next;
+ ($best, $best-score) = ($comb, $score)
+ if (! defined $best) || ($best-score < $score);
+ }
+ $best //= [];
+ return $best.sort({$^a <=> $^b}).reverse;
+}
+
+sub is-triangle ($x) {
+ my ($A, $B, $C) = @$x;
+ return 0 if
+ $A >= $B + $C
+ || $B >= $C + $A
+ || $C >= $A + $B;
+ return $A + $B + $C;
+}