-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsudoku.p6
More file actions
58 lines (48 loc) · 1.16 KB
/
Copy pathsudoku.p6
File metadata and controls
58 lines (48 loc) · 1.16 KB
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
use lib "lib";
use ProblemSolver;
my ProblemSolver $problem .= new;#: :stop-on-first-solution;
my @vars = (^9 X~ ^9).map: "var" ~ *;
my %page;
for ^9 -> $i {
my @cols = get().comb(/<[-\w]>/);
for ^@cols -> $j {
%page{"var$i$j"} = @cols[$j] unless @cols[$j] eq "-"
}
}
for @vars -> $var {
$problem.add-variable: $var, %page{$var} || 1 .. 9
}
sub print-sheet(%vals) {
my $c;
for @vars -> $var {
$c++;
if %vals{$var}:exists {
print %vals{$var}, " ";
} else {
print "- "
}
print " " if $c %% 3;
print "\n" if $c %% 9;
print "\n" if $c %% 27;
}
}
$problem.print-found = -> %values {
print "\e[0;0H\e[0J";
print-sheet(%values);
}
for ^9 -> $i {
$problem.unique-vars: (^9).map("var$i" ~ *)
}
for ^9 -> $j {
$problem.unique-vars: (^9).map("var" ~ * ~ $j)
}
for ^3 X ^3 -> ($i, $j) {
$problem.unique-vars: (
(3 * $i, 3 * $j), (3 * $i, 3 * $j + 1), (3 * $i, 3 * $j + 2),
(3 * $i + 1, 3 * $j), (3 * $i + 1, 3 * $j + 1), (3 * $i + 1, 3 * $j + 2),
(3 * $i + 2, 3 * $j), (3 * $i + 2, 3 * $j + 1), (3 * $i + 2, 3 * $j + 2),
).map(-> ($I, $J) {"var{$I}{$J}"})
}
my @resp = $problem.solve;
say "=" x 30, " ANSWER ", "=" x 30;
print-sheet $_ for @resp