aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2020-11-30 19:14:53 +0000
committerGitHub <noreply@github.com>2020-11-30 19:14:53 +0000
commit67b4c3af526957d33e824e99216b8ee4c8dc18c9 (patch)
treee944b4bb729b376ce77528271bc1bab056b6186a
parent5e711b47582551b3001954feddf4165d25addff9 (diff)
downloadperlweeklychallenge-club-67b4c3af526957d33e824e99216b8ee4c8dc18c9.tar.gz
perlweeklychallenge-club-67b4c3af526957d33e824e99216b8ee4c8dc18c9.tar.bz2
perlweeklychallenge-club-67b4c3af526957d33e824e99216b8ee4c8dc18c9.zip
Update ch-2.pl
This is adding in a new way of solving the problem...
-rw-r--r--challenge-089/james-smith/perl/ch-2.pl45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-089/james-smith/perl/ch-2.pl b/challenge-089/james-smith/perl/ch-2.pl
index 25befeef39..b9c086b9bb 100644
--- a/challenge-089/james-smith/perl/ch-2.pl
+++ b/challenge-089/james-smith/perl/ch-2.pl
@@ -32,3 +32,48 @@ sub magic {
}
return @solutions;
}
+
+### Now the bit below is another solution....
+
+## This looks at all the rules and applies them sequentially
+## You need to follow upwards
+
+## We can generate all values if we only have a, b & e
+## If we do this we get the sums of the 2nd and 3rd row as 3e & 30-3e respectively
+## For these to both be 15 e must be 5.
+
+## As e is 5 we only have to loop over a - $a and b - $_.
+
+print
+ ## Render output...
+ map {
+ map(
+ { " [ @{$_} ]\n" }
+ @{$_}
+ ),
+ "\n"
+ }
+ ## Get magic square....
+ map {
+ $a = $_;
+ ## Return square...
+ map { [
+ [ $a , $_ , -$a - $_ + 15 ],
+ [ -2*$a - $_ + 20, 5, 2*$a + $_ - 10 ],
+ [ $a + $_ - 5, -$_ + 10, -$a + 10 ]
+ ] }
+ ## Now we filter a/b...
+ grep { 20 > 2*$a + $_ } # f is a single digit
+ grep { 10 < 2*$a + $_ } # ------- " ---------
+ grep { 25 != 3*$a + 2*$_ } # a != f ( f = 2a - b - 10 )
+ grep { 20 != 3*$a + $_ } # a != d ( d = 20 - 2a - b )
+ grep { 15 != $a + 2*$_ } # c != b
+ grep { 15 != 2*$a + $_ } # c != a ( c = 15 - a - b )
+ grep { 10 != $a + $_ } # c != e ( 5 )
+ grep { 0 != -$a + $_ } # a != b
+ grep { 5 != $_ } # b != e ( 5 )
+ $a < 6 ? 6-$a .. 9 : 1 .. 14-$a # ensure c is a single digit number
+ }
+ 1..4 , 6..9; # a != e ( 5 )
+
+