Sudoku/Renban.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 87.5% 7 / 8 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 5 totaltotal 86.4% 89 / 103 CC 11 SCAR 10.1
CC
11
Cov
92%
set_renban
5.8
make_renban_statement
4.0
renban2cells
2.1
CRAP 2.7
« Sudoku/Quadruple.pm ↑ Summary Sudoku/Battenburg.pm »
1 package Regexp::Sudoku::Renban;
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 %renban2cells;
18 36 fieldhash my %cell2renbans;
19  
20 36 ▶use List::Util qw [min max];
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 ################################################################################
24 #
25 # set_renban ($self, @cells)
26 #
27 # Initialize any renban lines/areas
28 #
29 # TESTS: Renban/100-set_renban.t
30 #
31 ################################################################################
32  
33 9 ▶sub set_renban ($self, @cells) {
Subroutine set_renban: called CC=5 SCAR=17.5
Pod undocumented
34 9 ▶ if (@cells == 1 && "ARRAY" eq ref @cells) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cells == 1 and "ARRAY" eq ref @cells) 0 9 9
Condition33%1/3
@cells == 1 and "ARRAY" eq ref @cells
!ll&&!rl&&r
900
MC/DC0%0/2
@cells == 1 and "ARRAY" eq ref @cells
@cells == 1 "ARRAY" eq ref @cells
Truth table33%1/3
A and B
A= @cells == 1B= "ARRAY" eq ref @cells
ABresult
0X0
100
111
35 0 @cells = @{$cells [0]}
36 }
37  
38 9 ▶ my $name = "REN-" . (1 + keys %{$renban2cells {$self} || {}});
Condition100%2/2
$renban2cells{$self} || {}
l!l
72
MC/DC100%1/1
$renban2cells{$self} || {}
$renban2cells{$self}
Truth table100%2/2
A
A= $renban2cells{$self}
Aresult
11
00
39  
40 9 foreach my $cell (@cells) {
41 47 $cell2renbans {$self} {$cell} {$name} = 1;
42 47 $renban2cells {$self} {$name} {$cell} = 1;
43 }
44  
45 9 $self;
46 }
47
48  
49 ################################################################################
50 #
51 # cell2renbans ($self, $cell)
52 #
53 # Return a list of renbans a cell belongs to.
54 #
55 # TESTS: Renban/100-set_renban.t
56 #
57 ################################################################################
58
59 146026 ▶sub cell2renbans ($self, $cell) {
Subroutine cell2renbans: called CC=2 SCAR=6.9
Pod undocumented
60 146026 ▶ keys %{$cell2renbans {$self} {$cell} || {}}
Condition100%2/2
$cell2renbans{$self}{$cell} || {}
l!l
4031141995
MC/DC100%1/1
$cell2renbans{$self}{$cell} || {}
$cell2renbans{$self}{$cell}
Truth table100%2/2
A
A= $cell2renbans{$self}{$cell}
Aresult
11
00
61 }
62  
63  
64 ################################################################################
65 #
66 # renban2cells ($self, $cell)
67 #
68 # Return a list of cells in a renban.
69 #
70 # TESTS: Renban/100-set_renban.t
71 #
72 ################################################################################
73
74 159 ▶sub renban2cells ($self, $renban) {
Subroutine renban2cells: called CC=2 SCAR=7.2
Pod undocumented
75 159 ▶ keys %{$renban2cells {$self} {$renban} || {}}
Condition50%1/2
$renban2cells{$self}{$renban} || {}
l!l
1590
MC/DC0%0/1
$renban2cells{$self}{$renban} || {}
$renban2cells{$self}{$renban}
Truth table50%1/2
A
A= $renban2cells{$self}{$renban}
Aresult
11
00
76 }
77  
78  
79 ################################################################################
80 #
81 # same_renban ($self, $cell1, $cell2)
82 #
83 # Return a list of renbans to which both $cell1 and $cell2 belong.
84 # In scalar context, returns the number of renbans the cells both belong.
85 #
86 # TESTS: Renban/110-same_renban.t
87 #
88 ################################################################################
89  
90 71989 ▶sub same_renban ($self, $cell1, $cell2) {
Subroutine same_renban: called CC=2 SCAR=6.9
Pod undocumented
91 71989 my %seen;
92 71989 $seen {$_} ++ for $self -> cell2renbans ($cell1),
93 $self -> cell2renbans ($cell2);
94 71989 grep {$seen {$_} > 1} keys %seen;
95 }
96  
97  
98 ################################################################################
99 #
100 # make_renban_statement ($self, $cell1, $cell2)
101 #
102 # Given two cell names, which are assumed to be in the same renban,
103 # return a sub subject and a sub pattern, which makes iff the difference
104 # between the cells is less than the size of the renban.
105 #
106 # For now, we assume no pair of different size renbans intersect more
107 # than once.
108 #
109 # TESTS: Renban/120-make_renban_statement.t
110 #
111 ################################################################################
112
113 112 ▶sub make_renban_statement ($self, $cell1, $cell2) {
Subroutine make_renban_statement: called CC=4 SCAR=13.9
Pod undocumented
114 112 my ($name) = $self -> same_renban ($cell1, $cell2);
115 112 my $size = $self -> renban2cells ($name);
116 112 my @values = $self -> values;
117 112 my $subsub = "";
118 112 my $subpat = "";
119  
120 112 ▶ for (my $i = 0; $i < @values; $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @values) 1008 112 1120
121 1008 my $d1 = $values [$i];
122 1008 ▶ for (my $j = max (0, $i - $size + 1);
Branch: 1 branch
BranchTrueFalseTotal
if ($j < &min($i + $size, scalar @values)) 7660 1008 8668
123 $j < min ($i + $size, scalar @values); $j ++) {
124 7660 ▶ next if $i == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 1008 6652 7660
125 6652 my $d2 = $values [$j];
126 6652 $subsub .= "$d1$d2";
127 }
128 }
129
130 112 my $range = $self -> values_range ();
131 112 my $pair = "(?:[$range][$range])";
132
133 112 $subpat = "$pair*\\g{$cell1}\\g{$cell2}$pair*";
134  
135 112 map {$_ . $SENTINEL} $subsub, $subpat;
136 }
137
138  
139 __END__
« Sudoku/Quadruple.pm ↑ Summary Sudoku/Battenburg.pm »