1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#!/usr/bin/perl
# Perl Weekly Challenge - 089
# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-089/
#
# Task 2 - Magical Matrix
#
# Author: Niels 'PerlBoy' van Dijke
use strict;
use warnings;
# Unbuffered STDOUT
$|++;
use List::Util qw(sum shuffle);
use Memoize;
# Prototype
sub checkSolution(\@);
sub printSolution (\@);
memoize('sum', 'checkSolution');
# Give an extra command-line argument to invoke 'cheat-mode'
my @n= (1..4, 6..9);
push(@n, 5) unless (@ARGV);
my @s;
# Brute force try to find a solution
# (but memoize for some performance)
while (1) {
@s = shuffle(@n);
# splice '5' in the centre in 'cheat-mode'
splice(@s, 4, ,1, 5, $s[4]) if (@ARGV);
last if checkSolution(@s);
}
printSolution(@s);
sub checkSolution (\@) {
my ($ar) = @_;
return 1 if (
# rows
sum($ar->[0], $ar->[1], $ar->[2]) == 15 and
sum($ar->[3], $ar->[4], $ar->[5]) == 15 and
sum($ar->[6], $ar->[7], $ar->[8]) == 15 and
# columns
sum($ar->[0], $ar->[3], $ar->[6]) == 15 and
sum($ar->[1], $ar->[4], $ar->[7]) == 15 and
sum($ar->[2], $ar->[5], $ar->[8]) == 15 and
# diagonals
sum($ar->[0], $ar->[4], $ar->[8]) == 15 and
sum($ar->[2], $ar->[4], $ar->[6]) == 15
);
}
sub printSolution (\@) {
my ($ar) = @_;
for my $r (0..2) {
printf("%d %d %d\n",
$ar->[$r * 3],
$ar->[$r * 3 + 1],
$ar->[$r * 3 + 2]
);
}
}
|