| 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
Condition33%1/3
@cells == 1 and "ARRAY" eq ref @cells
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
| |||||||||||||||||||||||||||||
| 35 | 0 | @cells = @{$cells [0]} | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 9 | ▶ | my $name = "REN-" . (1 + keys %{$renban2cells {$self} || {}}); | ||||||||||||||||||||||||||
|
Condition100%2/2
$renban2cells{$self} || {}
MC/DC100%1/1
$renban2cells{$self} || {}
$renban2cells{$self}
Truth table100%2/2
A
A= $renban2cells{$self}
| |||||||||||||||||||||||||||||
| 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} || {}
MC/DC100%1/1
$cell2renbans{$self}{$cell} || {}
$cell2renbans{$self}{$cell}
Truth table100%2/2
A
A= $cell2renbans{$self}{$cell}
| |||||||||||||||||||||||||||||
| 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} || {}
MC/DC0%0/1
$renban2cells{$self}{$renban} || {}
$renban2cells{$self}{$renban}
Truth table50%1/2
A
A= $renban2cells{$self}{$renban}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 121 | 1008 | my $d1 = $values [$i]; | |||||||||||||||||||||||||||
| 122 | 1008 | ▶ | for (my $j = max (0, $i - $size + 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | $j < min ($i + $size, scalar @values); $j ++) { | ||||||||||||||||||||||||||||
| 124 | 7660 | ▶ | next if $i == $j; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||