diff options
| -rw-r--r-- | challenge-187/massa/raku/ch-1.raku | 41 | ||||
| -rw-r--r-- | challenge-187/massa/raku/ch-2.raku | 41 |
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-187/massa/raku/ch-1.raku b/challenge-187/massa/raku/ch-1.raku new file mode 100644 index 0000000000..4e0049622e --- /dev/null +++ b/challenge-187/massa/raku/ch-1.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku + +=begin pod + +Week 187: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-187 + +Task #1: Days Together + + Two friends, Foo and Bar gone on holidays seperately to the same city. You + are given their schedule i.e. C<start date> and C<end date>. + + To keep the task simple, the date is in the form C<DD-MM> and all dates + belong to the same calendar year i.e. between C<01-01> and C<31-12>. Also + the year is C<non-leap year> and both dates are inclusive. + + Write a script to find out for the given schedule, how many days they spent + together in the city, if at all. + +=end pod + +use Test; + +is 4, days-together <12-01 20-01>, <15-01 18-01>; +is 0, days-together <02-03 12-03>, <13-03 14-03>; +is 2, days-together <02-03 12-03>, <11-03 15-03>; +is 4, days-together <30-03 05-04>, <28-03 02-04>; + +done-testing; + +my sub day-number(Str $_) { + fail unless /(\d**2)\-(\d**2)/; + Date.new(2001, $/[1], $/[0]) - Date.new(2001, 1, 1) +} +sub days-together(@foo, @bar) { + my Set() $range-foo = Range.new: |(@foo.map: &day-number); + my Set() $range-bar = Range.new: |(@bar.map: &day-number); + elems $range-foo ∩ $range-bar +} + diff --git a/challenge-187/massa/raku/ch-2.raku b/challenge-187/massa/raku/ch-2.raku new file mode 100644 index 0000000000..f9031ada3f --- /dev/null +++ b/challenge-187/massa/raku/ch-2.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku + +=begin pod + +Week 187: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-187 + +Task #2: Magical Triplets + + You are given a list of positive numbers, C<@n>, having at least 3 numbers. + + Write a script to find the triplets C<(a, b, c)> from the given list that + satisfies the following rules. + + 1. a + b > c + 2. b + c > a + 3. a + c > b + 4. a + b + c is maximum. + + In case, you end up with more than one triplets having the maximum then + pick the triplet where a >= b >= c. + +=end pod + +use Test; + +is <3 2 2>, magical-triplets <1 2 3 2>; +is Nil, magical-triplets <1 3 2>; +is Nil, magical-triplets <1 1 2 3>; +is <4 3 2>, magical-triplets <4 3 2>; + +done-testing; + +sub magical-triplets(@n) { + my @triplets = @n.combinations(3).map( { |.permutations } ). + grep({ .[0] + .[1] > .[2] && .[1] + .[2] > .[0] && .[0] + .[2] > .[1] }). + sort({ not( .[0] ≥ .[1] ≥ .[2] ) }); + @triplets.map({ [+] $_ }).maxpairs.map({ @triplets[.key] }).head +} + |
