Magical Days Together with Triplets
Tags: perl, python, theweeklychallenge
Published: 2022-10-23 12:21:23

## Weekly Challenge 187

[Challenge](https://theweeklychallenge.org/blog/perl-weekly-challenge-187/), [My solution](https://github.com/manwar/perlweeklychallenge-club/tree/master/challenge-187/sgreen)

After a few interstate trips and a bout of COVID-19, it's now onward and upwards to the end of the year. The ICC Men's Twenty20 World Cup has also started here so it's time to change the [channel](https://www.t20worldcup.com/official-broadcasters) and throw out the remote for the next month. Go Black Caps!
 
## Task 1: Days Together

### Task

Two friends, Foo and Bar gone on holidays seperately[sic] to the same city. You are given their schedule i.e. start date and end date.

To keep the task simple, the date is in the form DD-MM and all dates belong to the same calendar year i.e. between 01-01 and 31-12. Also the year is 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.

### My solution

I really like this challenge, as it makes you think about how to solve them. Date math is never easy. For this task, I convert the four input dates into day of the year (1 = January 1<sup>st</sup>, 365 = December 31<sup>st</sup>). In Python, I use the [date](https://docs.python.org/3/library/datetime.html#datetime.date) module, while I use [Date::Calc](https://metacpan.org/pod/Date::Calc) for the Perl solution. This makes the rest of the solution relatively easy.

I check that both Foo and Bar don't leave before they arrive. We can calculate the number of days by calculating when the last person arrives and when the first person leaves. If this is a negative number, they didn't see each other. Others we take the value and add 1 to print the solution.

### Examples

```bash
$ ./ch-1.py 12-01 20-01 15-01 18-01
4 days

$ ./ch-1.py 02-03 12-03 13-03 14-03
0 days

$ ./ch-1.py 02-03 12-03 11-03 14-03
2 days

$ ./ch-1.py 30-03 05-04 28-03 02-04
4 days
```

## Task 2: Magical Triplets

### Task

You are given a list of positive numbers, @n, having at least 3 numbers.

Write a script to find the triplets (`a`, `b`, `c`) from the given list that satisfies the following rules.

1. a + b > c
1. b + c > a
1. a + c > b
1. 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.

### My solution

I'm sure there are smarter people doing this challenge that will come up with some formula for figuring out the quickest way to solve this, but some times the brute force approach is good enough. Especially when dealing with small number of things.

For this challenge, I use the combinations function from [itertools](https://docs.python.org/3.8/library/itertools.html#itertools.permutations) in Python and from [Algorithm::Combinatorics](https://metacpan.org/pod/Algorithm::Combinatorics) in Perl to compute all possible combinations of three digits.

I store the 'best' solution so far in the `solutions` list (which is set to None initially in Python, and an empty arrayref in Perl).

I then loop through each combination and determine if the first three criteria are met. If they and the solution value is empty or the sum is greater than the current `solution`, I set `solution` to the new list.

I finally print the result in the specified format.

### Examples

```bash
$ ./ch-2.py 1 2 3 2
(3, 2, 2)

$ ./ch-2.py 1 3 2
()

$ ./ch-2.py 1 1 2 3
()

$ ./ch-2.py 2 4 3
(4, 3, 2)
```