Chola and squares
Weekly challenge 109 — 19 April 2021
Week 109: 19 Apr 2021

You are given four squares as above with numbers named a, b, c, d, e, f, g.
Write a script to place the given numbers in the boxes so that sum of the numbers in each box is the same.
Example 1 Input: 1,2,3,4,5,6,7 Output: a = 6 b = 4 c = 1 d = 5 e = 2 f = 3 g = 7 Box 1: a + b = 6 + 4 = 10 Box 2: b + c + d = 4 + 1 + 5 = 10 Box 3: d + e + f = 5 + 2 + 3 = 10 Box 4: f + g = 3 + 7 = 10
This is an an interesting challenge. I pondered some shortcuts to the answer, but concluded that simply checking all the permutations of the given 7 numbers would be fast enough, and indeed that finds a solution in less than a second - or if there is no solution, meaning it has to try every permutation, it's just as fast.
Interestingly, the solution I found for the supplied 7 digits - 3, 7, 2, 1, 5, 4, 6 - is different from the one in the example.
I also tried using 7 random numbers in the range 0 - 20 and found quite a few solutions - around 5% of the ones I tried.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2021-04-19 use utf8; # Week 109 - task 2 - Four squares puzzle use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; use Algorithm::Combinatorics 'permutations'; four_squares_puzzle(1, 2, 3, 4, 5, 6, 7); four_squares_puzzle(3, 4, 5, 6, 7, 8, 9); four_squares_puzzle(4, 5, 6, 7, 8, 9, 10); four_squares_puzzle(5, 6, 7, 8, 9, 10, 11); four_squares_puzzle(13, 9, 5, 5, 8, 6, 14); four_squares_puzzle(1, 6, 16, 17, 5, 6, 17); sub four_squares_puzzle { my (@d, $i, $sum, $p, $a, $b, $c, $d, $e, $f, $g); # initialise @d = @_; say qq[\nInput: ] . join(', ', @d); $i = permutations(\@d); # loop over permutations while ($p = $i->next) { ($a, $b, $c, $d, $e, $f, $g) = @$p; $sum = $a + $b; # success? if ($b + $c + $d == $sum and $d + $e + $f == $sum and $f + $g == $sum) { say qq[Output: a = $a, b = $b, c = $c, d = $d, e = $e, f = $f, g = $g]; printf("a + b = %2d + %2d = %2d\n", $a, $b, $a + $b); printf("b + c + d = %2d + %2d + %2d = %2d\n", $b, $c, $d, $b + $c +$d); printf("d + e + f = %2d + %2d + %2d = %2d\n", $d, $e, $f, $d + $e + $f); printf("f + g = %2d + %2d = %2d\n", $f, $g, $f + $g); return; } } # failure say qq[Output: no solution found]; }
18 lines of code
I completed this challenge after the closing date
and it has not
been submitted to GitHub
Input: 1, 2, 3, 4, 5, 6, 7 Output: a = 3, b = 7, c = 2, d = 1, e = 5, f = 4, g = 6 a + b = 3 + 7 = 10 b + c + d = 7 + 2 + 1 = 10 d + e + f = 1 + 5 + 4 = 10 f + g = 4 + 6 = 10 Input: 3, 4, 5, 6, 7, 8, 9 Output: a = 7, b = 8, c = 3, d = 4, e = 5, f = 6, g = 9 a + b = 7 + 8 = 15 b + c + d = 8 + 3 + 4 = 15 d + e + f = 4 + 5 + 6 = 15 f + g = 6 + 9 = 15 Input: 4, 5, 6, 7, 8, 9, 10 Output: a = 9, b = 8, c = 5, d = 4, e = 6, f = 7, g = 10 a + b = 9 + 8 = 17 b + c + d = 8 + 5 + 4 = 17 d + e + f = 4 + 6 + 7 = 17 f + g = 7 + 10 = 17 Input: 5, 6, 7, 8, 9, 10, 11 Output: no solution found Input: 13, 9, 5, 5, 8, 6, 14 Output: a = 13, b = 6, c = 8, d = 5, e = 9, f = 5, g = 14 a + b = 13 + 6 = 19 b + c + d = 6 + 8 + 5 = 19 d + e + f = 5 + 9 + 5 = 19 f + g = 5 + 14 = 19 Input: 1, 6, 16, 17, 5, 6, 17 Output: a = 6, b = 17, c = 5, d = 1, e = 16, f = 6, g = 17 a + b = 6 + 17 = 23 b + c + d = 17 + 5 + 1 = 23 d + e + f = 1 + 16 + 6 = 23 f + g = 6 + 17 = 23
Any content of this website which has been created by Peter Campbell Smith is in the public domain