Skip to content

Instantly share code, notes, and snippets.

@lizmat
Created September 7, 2016 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizmat/b3fe91c5be759e5cdaaf7afdf2821cb4 to your computer and use it in GitHub Desktop.
Save lizmat/b3fe91c5be759e5cdaaf7afdf2821cb4 to your computer and use it in GitHub Desktop.
Magic number solution: runs at 25 wallclock as opposed to 35
my @numbers = 3 .. 11 ;
for @numbers.permutations { check_magic_box $_ }
sub check_magic_box(@a) {
say "@a[0], @a[1], @a[2]\n@a[3], @a[4], @a[5]\n@a[6], @a[7], @a[8]\n"
if 21
== @a[0] + @a[1] + @a[2]
== @a[3] + @a[4] + @a[5]
== @a[6] + @a[7] + @a[8]
== @a[0] + @a[3] + @a[6]
== @a[1] + @a[4] + @a[7]
== @a[2] + @a[5] + @a[8]
== @a[0] + @a[4] + @a[8]
== @a[6] + @a[4] + @a[2]
}
@timo
Copy link

timo commented Sep 7, 2016

#!/usr/bin/env perl6
my int @numbers = 3 .. 11 ;
for @numbers.permutations { check_magic_box $_  }

sub check_magic_box ( @array ) {
  #my ($a0, $a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8) = @array;
  my int $a0 = @array.AT-POS(0);
  my int $a1 = @array.AT-POS(1);
  my int $a2 = @array.AT-POS(2);
  my int $sum = 21 ;
  return 0 if $sum != $a0 + $a1 + $a2 ;
  my int $a3 = @array.AT-POS(3);
  my int $a4 = @array.AT-POS(4);
  my int $a5 = @array.AT-POS(5);
  return 0 if $sum != $a3 + $a4 + $a5 ;
  my int $a6 = @array.AT-POS(6);
  my int $a7 = @array.AT-POS(7);
  my int $a8 = @array.AT-POS(8);
  return 0 if $sum != $a6 + $a7 + $a8 ;
  return 0 if $sum != $a0 + $a3 + $a6 ;
  return 0 if $sum != $a1 + $a4 + $a7 ;
  return 0 if $sum != $a2 + $a5 + $a8 ;
  return 0 if $sum != $a0 + $a4 + $a8 ;
  return 0 if $sum != $a6 + $a4 + $a2 ;
  say "$a0, $a1, $a2" ;
  say "$a3, $a4, $a5" ;
  say "$a6, $a7, $a8" ;
  say '' ;
  return 1 ;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment