-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.pl
executable file
·217 lines (198 loc) · 5.8 KB
/
sudoku.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/perl
use Data::Dumper;
use constant SEGMENT_ROWS => 3;
use constant SEGMENT_COLS => 3;
use constant GRID_SIZE => SEGMENT_ROWS * SEGMENT_COLS;
use constant VALUES => 1..GRID_SIZE;
use constant SLOTS => 0..GRID_SIZE-1;
use constant CHARSET => ' 123456789ABCDEF';
my @chars = split('',CHARSET);
my $board = {
squares => [],
};
# read in the board
while (<STDIN>) {
chomp;
tr/a-z/A-Z/;
my @line = map { hex($_) } split('',$_);
push @{$board->{squares}}, \@line;
}
# validate the size of the board if you like...
showboard($board);
# Elimination round first: run two passes until we're finished or stuck
# - pass 1: determine possibilities for each empty square
# - pass 2: determine which possibilities are unique to a square
my $pass = 0;
my $filled;
my $unsolved;
do {
$unsolved = 0;
$filled = 0;
# Pass 1
$board->{poss} = [];
foreach my $row (SLOTS) {
foreach my $col (SLOTS) {
if ($board->{squares}->[$row][$col]) {
$board->{poss}->[$row][$col] = [ $board->{squares}->[$row][$col] ];
} else {
$board->{poss}->[$row][$col] = possibilities($board, $row, $col);
# print "Possibilities for [$row][$col]: [" . join("][",map { @chars[$_] } @{$board->{poss}->[$row][$col]}) . "]\n";
if (@{$board->{poss}->[$row][$col]} == 1) {
my ($val) = @{$board->{poss}->[$row][$col]};
print "[$row][$col] can only be [$val]\n";
$board->{squares}->[$row][$col] = $val;
$filled++;
} else {
$unsolved++;
}
}
}
}
# Pass 2
foreach my $row (SLOTS) {
foreach my $col (SLOTS) {
next if @{$board->{poss}->[$row][$col]} == 1;
foreach my $val (@{$board->{poss}->[$row][$col]}) {
my $uniq = { row => 1, col => 1, seg => 1 };
foreach my $i (SLOTS) {
if ($uniq->{row} && $col != $i) {
$uniq->{row} &&=
!grep { $_ == $val } @{$board->{poss}->[$row][$i]};
}
if ($uniq->{col} && $row != $i) {
$uniq->{col} &&=
!grep { $_ == $val } @{$board->{poss}->[$i][$col]};
}
}
my $h1 = int($row/SEGMENT_ROWS)*SEGMENT_ROWS;
my $h2 = $h1+SEGMENT_ROWS-1;
my $v1 = int($col/SEGMENT_COLS)*SEGMENT_COLS;
my $v2 = $v1+SEGMENT_COLS-1;
# print "Checking uniq for [$row][$col] in seg [$h1,v1]-[$h2,v2]\n";
foreach my $h ($h1..$h2) {
foreach my $v ($v1..$v2) {
next unless $uniq->{seg};
next if $row == $h && $col == $v;
$uniq->{seg} &&=
!grep { $_ == $val } @{$board->{poss}->[$h][$v]};
}
}
my @uniq_locs = grep { $uniq->{$_} } keys %$uniq;
if (@uniq_locs) {
print "[$row][$col] is the only location for $chars[$val] in this [" . join("][",@uniq_locs) . "]\n";
$board->{poss}->[$row][$col] = [ $val ];
$board->{squares}->[$row][$col] = $val;
$filled++;
$unsolved--;
last;
}
}
}
}
$pass++ if $filled;
print "Filled $filled squares on pass $pass ($unsolved unsolved squares remaining)\n";
showboard($board) if $filled;
} while ($unsolved && $filled);
# If the elimination round didn't solve the puzzle, use lookahead
if ($unsolved) {
print "Looks like we got stuck after $pass passes, using lookahead.\n";
# make a list of unsolved squares and possibilities
my @nodes;
foreach my $row (SLOTS) {
foreach my $col (SLOTS) {
next if @{$board->{poss}->[$row][$col]} == 1;
my $node = {
row => $row,
col => $col,
poss => undef,
try => undef,
};
push @nodes, $node;
}
}
@nodes = sort { @{$board->{poss}->[$a->{row}][$a->{col}]} <=> @{$board->{poss}->[$b->{row}][$b->{col}]} } @nodes;
# try the possibility graph until the puzzle is solved or stuck
my $i = 0;
my $pass = 0;
while ($i>=0 && $i<=$#nodes) {
$pass++;
# if we've already started a possibility set, continue
my $row = $nodes[$i]->{row};
my $col = $nodes[$i]->{col};
my $poss = $nodes[$i]->{poss};
if (defined($poss)) {
my $try = $nodes[$i]->{try};
$try++;
if ($try < @$poss) {
# try the next one
$board->{squares}->[$row][$col] = $poss->[$try];
$nodes[$i]->{try} = $try;
$i++;
} else {
# reset this node and move back up the tree
$board->{squares}->[$row][$col] = 0;
$nodes[$i]->{poss} = undef;
$i--;
}
} else {
# determine new set of possibilities
$poss = possibilities($board, $row, $col);
if (@$poss) {
$nodes[$i]->{poss} = $poss;
$nodes[$i]->{try} = 0;
$board->{squares}->[$row][$col] = $poss->[0];
$i++;
} else {
# failed, reset this node and move back up the tree
$board->{squares}->[$row][$col] = 0;
$nodes[$i]->{poss} = undef;
$i--;
}
}
}
if ($i < 0) {
print "The board seems to be stuck after $pass lookahead passes.\n";
} else {
print "The board is solved after $pass lookahead passes.\n";
}
} else {
print "Solved the puzzle in $pass passes.\n";
}
showboard($board);
sub possibilities {
my ($board, $row, $col) = @_;
my %taken = map { $_ => 0 } VALUES;
foreach my $i (SLOTS) {
# Check the values in this row
$taken{$board->{squares}->[$row][$i]} = 1;
# Check the values in this column
$taken{$board->{squares}->[$i][$col]} = 1;
}
# Check the values in this segment
my $h1 = int($row/SEGMENT_ROWS)*SEGMENT_ROWS;
my $h2 = $h1+SEGMENT_ROWS-1;
my $v1 = int($col/SEGMENT_COLS)*SEGMENT_COLS;
my $v2 = $v1+SEGMENT_COLS-1;
# print "Checking segment [$h1][$v1] - [$h2][$v2] for [$row][$col]\n";
foreach my $h ($h1..$h2) {
foreach my $v ($v1..$v2) {
$taken{$board->{squares}->[$h][$v]} = 1;
}
}
return [ grep { !$taken{$_} } VALUES ];
}
sub showboard {
my $board = shift;
my $row = 1;
foreach my $line (@{$board->{squares}}) {
my $col = 1;
foreach my $square (@$line) {
print '[' . $chars[$square] . ']';
print ' ' if $col != VALUES && $col % SEGMENT_COLS == 0;
$col++;
}
print "\n";
print "\n" if $row != VALUES && $row % SEGMENT_ROWS == 0;
$row++;
}
}