-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-2.pl
133 lines (102 loc) · 2.68 KB
/
ch-2.pl
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say);
use List::MoreUtils qw(first_index);
use Data::Dumper::OneLine;
# Input
my @N = (
[0, 0, 0, 2, 6, 0, 7, 0, 1],
[6, 8, 0, 0, 7, 0, 0, 9, 0],
[1, 9, 0, 0, 0, 4, 5, 0, 0],
[8, 2, 0, 1, 0, 0, 0, 4, 0],
[0, 0, 4, 6, 0, 2, 9, 0, 0],
[0, 5, 0, 0, 0, 3, 0, 2, 8],
[0, 0, 9, 3, 0, 0, 0, 7, 4],
[0, 4, 0, 0, 5, 0, 0, 3, 6],
[7, 0, 3, 0, 1, 8, 0, 0, 0]
);
# Solve
my %safes = createSafes();
solve();
# Output
say Dumper($_) for(@N);
# Solve sudoku game
sub solve {
my $row = -1;
my $col = -1;
for (my $i = 0; $i < 9; $i++) {
my $index = first_index { $_ == 0 } @{$N[$i]};
if ($index > -1) {
$row = $i;
$col = $index;
last;
}
}
return 1 if ($col == -1);
my $result = 0;
for (my $val = 1; $val <= 9; $val++) {
if (isSafe($row, $col, $val)) {
addSafeValue($row, $col, $val);
$result = solve();
last if ($result);
removeSafeValue($row, $col, $val);
}
}
return $result;
}
# Test if element is safe
sub isSafe {
my ($row, $col, $val) = @_;
return 0 if (grep(/^$val$/, @{$safes{$row}{9}}));
return 0 if (grep(/^$val$/, @{$safes{9}{$col}}));
return 0 if (grep(/^$val$/, @{$safes{squareIndex($row)}{squareIndex($col)}}));
return 1;
}
# Create and fill safe list
sub createSafes {
my %result = ();
for(my $i = 0; $i < 9; $i++) {
$result{$i}{9} = [];
for(my $j = 0; $j < 9; $j++) {
$result{9}{$j} = [] if($i == 0);
my $val = $N[$i][$j];
my $iSquare = squareIndex($i);
my $jSquare = squareIndex($j);
$result{$iSquare}{$jSquare} = [] if(!defined $result{$iSquare}{$jSquare});
if($val != 0) {
push @{$result{9}{$j}}, $val;
push @{$result{$i}{9}}, $val;
push @{$result{$iSquare}{$jSquare}}, $val;
}
}
}
return %result;
}
# Add safe element
sub addSafeValue {
my ($row, $col, $val) = @_;
$N[$row][$col] = $val;
push @{$safes{$row}{9}}, $val;
push @{$safes{9}{$col}}, $val;
push @{$safes{squareIndex($row)}{squareIndex($col)}}, $val;
}
# Remove safe alement
sub removeSafeValue {
my ($row, $col, $val) = @_;
my $squareRow = squareIndex($row);
my $squareCol = squareIndex($col);
$N[$row][$col] = 0;
my $indexRow = first_index { $_ == $val } @{$safes{$row}{9}};
splice(@{$safes{$row}{9}}, $indexRow, 1);
my $indexCol = first_index { $_ == $val } @{$safes{9}{$col}};
splice(@{$safes{9}{$col}}, $indexCol, 1);
my $indexSquare = first_index { $_ == $val } @{$safes{$squareRow}{$squareCol}};
splice(@{$safes{$squareRow}{$squareCol}}, $indexSquare, 1);
}
# Get square index
sub squareIndex {
return 2 if ($_[0] < 3);
return 5 if ($_[0] < 6);
return 8;
}