PWC 187

PWC 187

Challenge 1 (Days Together)

Two friends Foo and Bar vacation in the same city, with the start-dates and end-dates for each one's vacation given in the form 'dd-mm'. The year is assumed to be the same non-leap year for all dates. The task is to work out the number of days when Foo and Bar are both in the city at the same time. 

This resolves to finding the number of days between the later start-date and the earlier end-date, including both days in the count (e.g., if they are both the same day, then the count is 1, not zero). This is easy to do with the numerous Date modules in Perl 5, or the built-in Date support in Raku and Julia. 

In Perl 5, I used the lightweight fast Date::Calc module exporting the Delta_Days subroutine. Delta_Days counts days between two (year, month, day) triplets, input as a flat list. Since it excludes the second (ending) date from the count, I add 1 to include it for the present task.

My Perl 5 script uses simple helper subroutines to parse the 'dd-mm' string, and to find the earlier or the later of two dates.

I use 2022 as the common year for the dates. Any non-leap year will do.

My Raku script is derived from the Perl 5 one. In Raku, the helper subroutines are not needed, except for 'parse-date'. Date::Calc is not needed either, and one just uses the regular arithmetic operators + and - for Date arithmetic.

My Julia script is a direct translation of the Raku one.


Challenge 2 (Magical Triplets)

We are given a list of positive numbers @n say that contains at least three numbers.

We are asked to find the three-number sub-list(s) of the list, say (a,b,c) that satisfy the following criteria:

  1. a + b  >  c
  2. b + c  > a
  3. a + c  > b
  4. (a + b + c) is maximum.

We are asked to return the sub-list(s) sorted in descending order.

I assume that "maximum" in 4. means "maximum among all possible triplets that are sub-lists of  @n". An alternative interpretation could be "maximum among all triplet sub-lists of @n that satisfy 1 through 3". The test examples given do not distinguish between these two interpretations.

Sticking to my assumptions, the task reduces to:

  1. Find the three largest elements of @n. Their sum is automatically the maximum of any triplet sub-list. Let this sub-list be @candidate. An easy way to find @candidate is to sort @n in descending order and take the first three elements.
  2. Test if any element of @candidate is greater than or equal to the sum of the other two. If yes (some element is greater), then return an empty array. If not, then @candidate is the answer.

This is a straightforward exercise in all three languages.

The logic actually simplifies further, as fellow contributor Athanasius explains in their somewhat similar Perl 5 solution (see the detailed explanation at the start). In step 2 above, we only need to test if b + c > a. Since the triple (a, b, c) is already sorted in descending order, a >= b >= c, it follows directly that a + b > c and a + c > b. No need to check these. 

Athanasius has the alternative interpretation of 'maximum'. They implement this by sliding down the sorted @n if the first triple fails to work. They stop when they get a triple that satisfies the first three test conditions, or they have reached the end of the array.

Comments

Popular posts from this blog

PWC 258

PWC 249

PWC 253