## Perl Weekly Challenge 043: Olympic Rings and Self-Descriptive Numbers

## Olympic Rings

There are5 ringsin the Olympic Logo [as shown below]. They are colour coded as in Blue, Black, Red, Yellow and Green. We have allocated some numbers to these rings as below: Blue: 8, Yellow: 7, Green: 5, Red: 9. The Black ring is empty currently. You are given the numbers 1, 2, 3, 4 and 6. Write a script to place these numbers in the rings so that the sum of numbers in each ring is exactly 11.

My first idea was to go over all the possible permutation of the numbers and report those that satisfy the sum condition. I chose Math::Combinatorics as the module to handle the permutations.

```
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use Math::Combinatorics;
my $SUM = 11;
my ($red, $green, $yellow, $blue) = (9, 5, 7, 8);
my $mc = 'Math::Combinatorics'->new(data => [1, 2, 3, 4, 6]);
while (my ($black, $red_green, $black_green, $black_yellow, $blue_yellow)
= $mc->next_permutation
) {
my @sums = ($red + $red_green,
$green + $red_green + $black_green,
$black + $black_green + $black_yellow,
$yellow + $black_yellow + $blue_yellow,
$blue + $blue_yellow);
say join ' ',
$red_green, $black_green, $black, $black_yellow, $blue_yellow
unless grep $_ != $SUM, @sums;
}
```

It tries all the 120 possible permutations, but from a computer point of view, it’s not so many. While finishing the solution, I already saw it could be solved in a much faster and straightforward way.