Sudoku/Battenburg.pm

filter: Statementstmt 92.5% 75 / 81 Branchbran 100% 2 / 2 Conditioncond 72.7% 8 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 10 totaltotal 82.1% 106 / 129 CC 13 SCAR 9.1
CC
13
Cov
90%
battenburg2cells
6.0
same_anti_battenburg
6.0
set_battenburg
3.0
CRAP 2.5
« Sudoku/Renban.pm ↑ Summary Sudoku/Utils.pm »
1 package Regexp::Sudoku::Battenburg;
2  
3 36 ▶use 5.028;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 36 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 36 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 36 ▶use experimental 'lexical_subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 36 our $VERSION = '2022030401';
13  
14 36 ▶use Hash::Util::FieldHash qw [fieldhash];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 36 ▶use Regexp::Sudoku::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 36 fieldhash my %battenburg2cells;
18 36 fieldhash my %cell2battenburgs;
19 36 fieldhash my %anti_battenburg2cells;
20 36 fieldhash my %cell2anti_battenburgs;
21  
22 36 ▶use List::Util qw [min max];
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24
25 ################################################################################
26 #
27 # set_battenburg ($self, @cells)
28 #
29 # Set one or more Battenburg constraints. For each constraint, we give
30 # the top left cell. (Multiple cells mean *different* constraints, not
31 # the cells of a single constraint)
32 #
33 # TESTS: Battenburg/100-set_battenburg.t
34 #
35 ################################################################################
36
37 1 ▶sub set_battenburg ($self, @cells) {
Subroutine set_battenburg: called CC=3 SCAR=11.0
Pod undocumented
38 1 foreach my $name (@cells) {
39 #
40 # Calculate all the cells of the constraint
41 #
42 6 my ($r, $c) = cell_row_column ($name);
43 6 my @cells = (cell_name ($r, $c), cell_name ($r, $c + 1),
44 cell_name ($r + 1, $c), cell_name ($r + 1, $c + 1));
45 6 foreach my $cell (@cells) {
46 24 $cell2battenburgs {$self} {$cell} {$name} = 1;
47 24 $battenburg2cells {$self} {$name} {$cell} = 1;
48 }
49 }
50 1 $self;
51 }
52
53
54 ################################################################################
55 #
56 # set_anti_battenburg ($self, @cells)
57 #
58 # Set one or more anti Battenburg constraints. For each constraint, we give
59 # the top left cell. (Multiple cells mean *different* constraints, not
60 # the cells of a single constraint)
61 #
62 # TESTS: Battenburg/105-set_anti_battenburg.t
63 #
64 ################################################################################
65
66 1 ▶sub set_anti_battenburg ($self, @cells) {
Subroutine set_anti_battenburg: called CC=3 SCAR=11.0
Pod undocumented
67 1 foreach my $name (@cells) {
68 #
69 # Calculate all the cells of the constraint
70 #
71 10 my ($r, $c) = cell_row_column ($name);
72 10 my @cells = (cell_name ($r, $c), cell_name ($r, $c + 1),
73 cell_name ($r + 1, $c), cell_name ($r + 1, $c + 1));
74 10 foreach my $cell (@cells) {
75 40 $cell2anti_battenburgs {$self} {$cell} {$name} = 1;
76 40 $anti_battenburg2cells {$self} {$name} {$cell} = 1;
77 }
78 }
79 1 $self;
80 }
81
82
83 ################################################################################
84 #
85 # cell2battenburgs ($self, $cell)
86 #
87 # Return a list of Battenburgs a cell belongs to.
88 #
89 # TESTS: Battenburg/100-set_battenburg.t
90 #
91 ################################################################################
92
93 143754 ▶sub cell2battenburgs ($self, $cell) {
Subroutine cell2battenburgs: called CC=2 SCAR=6.9
Pod undocumented
94 143754 ▶ keys %{$cell2battenburgs {$self} {$cell} || {}}
Condition100%2/2
$cell2battenburgs{$self}{$cell} || {}
l!l
1720142034
MC/DC100%1/1
$cell2battenburgs{$self}{$cell} || {}
$cell2battenburgs{$self}{$cell}
Truth table100%2/2
A
A= $cell2battenburgs{$self}{$cell}
Aresult
11
00
95 }
96
97  
98 ################################################################################
99 #
100 # cell2anti_battenburgs ($self, $cell)
101 #
102 # Return a list of anti-Battenburgs a cell belongs to.
103 #
104 # TESTS: Battenburg/105-set_anti_battenburg.t
105 #
106 ################################################################################
107
108 2048 ▶sub cell2anti_battenburgs ($self, $cell) {
Subroutine cell2anti_battenburgs: called CC=2 SCAR=6.9
Pod undocumented
109 2048 ▶ keys %{$cell2anti_battenburgs {$self} {$cell} || {}}
Condition100%2/2
$cell2anti_battenburgs{$self}{$cell} || {}
l!l
372011
MC/DC100%1/1
$cell2anti_battenburgs{$self}{$cell} || {}
$cell2anti_battenburgs{$self}{$cell}
Truth table100%2/2
A
A= $cell2anti_battenburgs{$self}{$cell}
Aresult
11
00
110 }
111
112  
113 ################################################################################
114 #
115 # battenburg2cells ($self, $name)
116 #
117 # Return a list of cells in a Battenburg.
118 #
119 # TESTS: Battenburg/100-set_battenburg.t
120 #
121 ################################################################################
122
123 0 ▶sub battenburg2cells ($self, $name) {
Subroutine battenburg2cells: not called CC=2 SCAR=17.9
Pod undocumented
124 0 ▶ keys %{$battenburg2cells {$self} {$name} || {}}
Condition0%0/2
$battenburg2cells{$self}{$name} || {}
l!l
00
MC/DC0%0/1
$battenburg2cells{$self}{$name} || {}
$battenburg2cells{$self}{$name}
Truth table0%0/2
A
A= $battenburg2cells{$self}{$name}
Aresult
11
00
125 }
126
127  
128 ################################################################################
129 #
130 # anti_battenburg2cells ($self, $name)
131 #
132 # Return a list of cells in an anti-Battenburg.
133 #
134 # TESTS: Battenburg/105-set_anti_battenburg.t
135 #
136 ################################################################################
137
138 40 ▶sub anti_battenburg2cells ($self, $name) {
Subroutine anti_battenburg2cells: called CC=2 SCAR=7.2
Pod undocumented
139 40 ▶ keys %{$anti_battenburg2cells {$self} {$name} || {}}
Condition50%1/2
$anti_battenburg2cells{$self}{$name} || {}
l!l
400
MC/DC0%0/1
$anti_battenburg2cells{$self}{$name} || {}
$anti_battenburg2cells{$self}{$name}
Truth table50%1/2
A
A= $anti_battenburg2cells{$self}{$name}
Aresult
11
00
140 }
141
142  
143 ################################################################################
144 #
145 # same_battenburg ($self, $cell1, $cell2)
146 #
147 # Return a list of Battenburgs to which both $cell1 and $cell2 belong.
148 # In scalar context, returns the number of Battenburgs the cells both belong.
149 #
150 # TESTS: Battenburg/110-same_battenburg.t
151 #
152 ################################################################################
153  
154 71877 ▶sub same_battenburg ($self, $cell1, $cell2) {
Subroutine same_battenburg: called CC=2 SCAR=6.9
Pod undocumented
155 71877 my %seen;
156 71877 $seen {$_} ++ for $self -> cell2battenburgs ($cell1),
157 $self -> cell2battenburgs ($cell2);
158  
159 71877 grep {$seen {$_} > 1} keys %seen;
160 }
161
162  
163 ################################################################################
164 #
165 # same_anti_battenburg ($self, $cell1, $cell2)
166 #
167 # Return a list of anti-Battenburgs to which both $cell1 and $cell2 belong.
168 # In scalar context, returns the number of anti-battenburgs the cells
169 # both belong.
170 #
171 # TESTS: Battenburg/115-same_anti_battenburg.t
172 #
173 ################################################################################
174  
175 0 ▶sub same_anti_battenburg ($self, $cell1, $cell2) {
Subroutine same_anti_battenburg: not called CC=2 SCAR=17.9
Pod undocumented
176 0 my %seen;
177 0 $seen {$_} ++ for $self -> cell2anti_battenburgs ($cell1),
178 $self -> cell2anti_battenburgs ($cell2);
179  
180 0 grep {$seen {$_} > 1} keys %seen;
181 }
182  
183  
184 ################################################################################
185 #
186 # make_battenburg_statement ($self, $cell1, $cell2)
187 #
188 # Return a statement which implements a Battenburg constraint between
189 # the two cells. We will assume the given cells belong to the same
190 # Battenburg contraint. If the cells are on the same row or column,
191 # the constraint is that they have a different parity. Else, the
192 # cells must have the same parity.
193 #
194 # TESTS: Battenburg/120-make_battenburg_statement.t
195 #
196 ################################################################################
197  
198 35 ▶sub make_battenburg_statement ($self, $cell1, $cell2) {
Subroutine make_battenburg_statement: called CC=3 SCAR=11.0
Pod undocumented
199 35 my ($r1, $c1) = cell_row_column ($cell1);
200 35 my ($r2, $c2) = cell_row_column ($cell2);
201 35 my ($subsub, $subpat);
202
203 #
204 # Case 1, cells are diagonally opposite.
205 # Then the parity must be the same.
206 #
207 35 ▶ if ($r1 != $r2 && $c1 != $c2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r1 != $r2 and $c1 != $c2) { } 12 23 35
Condition100%3/3
$r1 != $r2 and $c1 != $c2
!ll&&!rl&&r
121112
MC/DC100%2/2
$r1 != $r2 and $c1 != $c2
$r1 != $r2 $c1 != $c2
Truth table100%3/3
A and B
A= $r1 != $r2B= $c1 != $c2
ABresult
0X0
100
111
208 12 my $md = $self -> must_differ ($cell1, $cell2);
209 12 return $self -> make_same_parity_statement ($cell1, $cell2, $md);
210 }
211 else {
212 23 return $self -> make_different_parity_statement ($cell1, $cell2);
213 }
214 }
215  
216  
217 ################################################################################
218 #
219 # make_anti_battenburg_statement ($self, $anti_battenburg)
220 #
221 # Return a (set of) statement(s) which implements the constraints
222 # for an anti-Battenburg. This will be four constraints, of which
223 # only one needs to be true (exactly 1 is not possible, it's either 0,
224 # 2, or 4 -- but all we care about is that it's not 0).
225 #
226 # TESTS: Battenburg/125-make_anti_battenburg_statement.t
227 #
228 ################################################################################
229  
230 10 ▶sub make_anti_battenburg_statement ($self, $anti_battenburg) {
Subroutine make_anti_battenburg_statement: called CC=1 SCAR=0.0
Pod undocumented
231 #
232 # We will make use of the fact that an anti-Battenburg is
233 # identified with it's top-left cell. We can just calculate
234 # the other cells.
235 #
236 10 my $cell1 = $anti_battenburg;
237 10 my ($r1, $c1) = cell_row_column ($cell1); # Top-left
238 10 my ($r2, $c2) = ($r1, $c1 + 1); # Top-right
239 10 my ($r3, $c3) = ($r1 + 1, $c1 + 1); # Bottom-right
240 10 my ($r4, $c4) = ($r1 + 1, $c1); # Bottom-left
241 10 my $cell2 = cell_name ($r2, $c2);
242 10 my $cell3 = cell_name ($r3, $c3);
243 10 my $cell4 = cell_name ($r4, $c4);
244  
245 10 my $subsub = $self -> make_same_parity_subject;
246 10 my $range = $self -> values_range (1);
247 10 my $subpat = "[$range]*(?:\\g{$cell1}\\g{$cell2}|" .
248 "\\g{$cell2}\\g{$cell3}|" .
249 "\\g{$cell3}\\g{$cell4}|" .
250 "\\g{$cell4}\\g{$cell1})[$range]*";
251  
252 10 map {$_ . $SENTINEL} $subsub, $subpat;
253 }
254  
255  
256 __END__
« Sudoku/Renban.pm ↑ Summary Sudoku/Utils.pm »