| 1 | package Regexp::Sudoku::Parity; | ||||||||||||||||||||||||||||
| 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 %evens; | |||||||||||||||||||||||||||
| 18 | 36 | fieldhash my %odds; | |||||||||||||||||||||||||||
| 19 | 36 | fieldhash my %is_even; | |||||||||||||||||||||||||||
| 20 | 36 | fieldhash my %is_odd; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 36 | ▶ | use List::Util qw [min max]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | ################################################################################ | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | # init_evens_odds ($self) | ||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||
| 30 | # Initialize the even and odd values of the sudoku. Only called when we're | ||||||||||||||||||||||||||||
| 31 | # actually needing the even or odds values. | ||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # TESTS: Parity/010_evens_odds.t | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | ################################################################################ | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 215 | ▶ | sub init_even_odds ($self) { | ||||||||||||||||||||||||||
|
Subroutine
init_even_odds: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 215 | ▶ | return $self if $evens {$self} || $odds {$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$evens{$self} or $odds{$self}
MC/DC50%1/2
$evens{$self} or $odds{$self}
$evens{$self}
$odds{$self}
Truth table66%2/3
A or B
A= $evens{$self}B= $odds{$self}
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 5 | my $values = $self -> values; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 5 | my $evens = do {my $i = 1; join "" => grep {$i = !$i} split // => $values}; | |||||||||||||||||||||||||||
| 43 | 5 | my $odds = do {my $i = 0; join "" => grep {$i = !$i} split // => $values}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 5 | $evens {$self} = $evens; | |||||||||||||||||||||||||||
| 46 | 5 | $odds {$self} = $odds; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ################################################################################ | ||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||
| 55 | # evens ($self) | ||||||||||||||||||||||||||||
| 56 | # | ||||||||||||||||||||||||||||
| 57 | # Return the set of even values used in the sudoku. In list context, this will | ||||||||||||||||||||||||||||
| 58 | # be an array of characters; in scalar context, a string. | ||||||||||||||||||||||||||||
| 59 | # | ||||||||||||||||||||||||||||
| 60 | # TESTS: Parity/010_evens_odds.t | ||||||||||||||||||||||||||||
| 61 | # | ||||||||||||||||||||||||||||
| 62 | ################################################################################ | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 112 | ▶ | sub evens ($self) { | ||||||||||||||||||||||||||
|
Subroutine
evens: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 112 | $self -> init_even_odds; | |||||||||||||||||||||||||||
| 66 | 112 | ▶ | wantarray ? split // => $evens {$self} : $evens {$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | ################################################################################ | ||||||||||||||||||||||||||||
| 71 | # | ||||||||||||||||||||||||||||
| 72 | # odds ($self) | ||||||||||||||||||||||||||||
| 73 | # | ||||||||||||||||||||||||||||
| 74 | # Return the set of odd values used in the sudoku. In list context, this will | ||||||||||||||||||||||||||||
| 75 | # be an array of characters; in scalar context, a string. | ||||||||||||||||||||||||||||
| 76 | # | ||||||||||||||||||||||||||||
| 77 | # TESTS: Parity/010_evens_odds.t | ||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||
| 79 | ################################################################################ | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 103 | ▶ | sub odds ($self) { | ||||||||||||||||||||||||||
|
Subroutine
odds: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 103 | $self -> init_even_odds; | |||||||||||||||||||||||||||
| 83 | 103 | ▶ | wantarray ? split // => $odds {$self} : $odds {$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ################################################################################ | ||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||
| 89 | # set_is_even ($self, $cell) { | ||||||||||||||||||||||||||||
| 90 | # set_is_odd ($self, $cell) { | ||||||||||||||||||||||||||||
| 91 | # is_even ($self, $cell) | ||||||||||||||||||||||||||||
| 92 | # is_odd ($self, $cell) | ||||||||||||||||||||||||||||
| 93 | # | ||||||||||||||||||||||||||||
| 94 | # Set whether a cell is to be even/odd, or return this. | ||||||||||||||||||||||||||||
| 95 | # | ||||||||||||||||||||||||||||
| 96 | # TESTS: Parity/100-is_even_odd.t | ||||||||||||||||||||||||||||
| 97 | # | ||||||||||||||||||||||||||||
| 98 | ################################################################################ | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 67 | ▶ | sub set_is_even ($self, $cell) {$is_even {$self} {$cell} = 1; $self;} | ||||||||||||||||||||||||||
|
Subroutine
set_is_even: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 58 | ▶ | sub set_is_odd ($self, $cell) {$is_odd {$self} {$cell} = 1; $self;} | ||||||||||||||||||||||||||
|
Subroutine
set_is_odd: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 3522 | ▶ | sub is_even ($self, $cell) {$is_even {$self} {$cell}} | ||||||||||||||||||||||||||
|
Subroutine
is_even: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 3406 | ▶ | sub is_odd ($self, $cell) {$is_odd {$self} {$cell}} | ||||||||||||||||||||||||||
|
Subroutine
is_odd: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | ################################################################################ | ||||||||||||||||||||||||||||
| 107 | # | ||||||||||||||||||||||||||||
| 108 | # make_even_statement ($cell) | ||||||||||||||||||||||||||||
| 109 | # make_odd_statement ($cell) | ||||||||||||||||||||||||||||
| 110 | # | ||||||||||||||||||||||||||||
| 111 | # Given a cell name, return a sub subject and a sub pattern allowing the | ||||||||||||||||||||||||||||
| 112 | # cell to pick up one of the even or odd values in the sudoku. | ||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||
| 114 | # TESTS: Parity/200-make_even_odd_statement.t | ||||||||||||||||||||||||||||
| 115 | # 120-make_cell_statement.t | ||||||||||||||||||||||||||||
| 116 | # | ||||||||||||||||||||||||||||
| 117 | ################################################################################ | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 67 | ▶ | sub make_even_statement ($self, $cell) { | ||||||||||||||||||||||||||
|
Subroutine
make_even_statement: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 67 | $self -> make_empty_statement ($cell, "evens") | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 58 | ▶ | sub make_odd_statement ($self, $cell) { | ||||||||||||||||||||||||||
|
Subroutine
make_odd_statement: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 58 | $self -> make_empty_statement ($cell, "odds") | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | ################################################################################ | ||||||||||||||||||||||||||||
| 128 | # | ||||||||||||||||||||||||||||
| 129 | # make_same_parity_statement ($self, $cell1, $cell2, $must_differ = 0) | ||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||
| 131 | # Return a statement which forces the two cells to have the same parity | ||||||||||||||||||||||||||||
| 132 | # (so, both cells are either even, or both cells are odd). Optionally, | ||||||||||||||||||||||||||||
| 133 | # we can also force the cells to be different. | ||||||||||||||||||||||||||||
| 134 | # | ||||||||||||||||||||||||||||
| 135 | # TESTS: Parity/200-make_same_parity_statement.t | ||||||||||||||||||||||||||||
| 136 | # | ||||||||||||||||||||||||||||
| 137 | ################################################################################ | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 22 | ▶ | sub make_same_parity_subject ($self, $must_differ = 0) { | ||||||||||||||||||||||||||
|
Subroutine
make_same_parity_subject: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 22 | my $e = semi_debruijn_seq (scalar $self -> evens, !$must_differ); | |||||||||||||||||||||||||||
| 141 | 22 | my $o = semi_debruijn_seq (scalar $self -> odds, !$must_differ); | |||||||||||||||||||||||||||
| 142 | 22 | "${e}0${o}"; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 12 | ▶ | sub make_same_parity_statement ($self, $cell1, $cell2, $must_differ = 0) { | ||||||||||||||||||||||||||
|
Subroutine
make_same_parity_statement: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | 12 | my $range = $self -> values_range (1); | |||||||||||||||||||||||||||
| 147 | 12 | my $subpat = "[$range]*\\g{$cell1}\\g{$cell2}[$range]*"; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 12 | map {$_ . $SENTINEL} $self -> make_same_parity_subject ($must_differ), | |||||||||||||||||||||||||||
| 150 | $subpat; | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | ################################################################################ | ||||||||||||||||||||||||||||
| 155 | # | ||||||||||||||||||||||||||||
| 156 | # make_different_parity_statement ($self, $cell1, $cell2) | ||||||||||||||||||||||||||||
| 157 | # | ||||||||||||||||||||||||||||
| 158 | # Return a statement which forces the two cells to have different parity. | ||||||||||||||||||||||||||||
| 159 | # (so, one cell is odd, the other even). | ||||||||||||||||||||||||||||
| 160 | # | ||||||||||||||||||||||||||||
| 161 | # TESTS: Parity/210-make_different_parity_statement.t | ||||||||||||||||||||||||||||
| 162 | # | ||||||||||||||||||||||||||||
| 163 | ################################################################################ | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 23 | ▶ | sub make_different_parity_statement ($self, $cell1, $cell2) { | ||||||||||||||||||||||||||
|
Subroutine
make_different_parity_statement: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | 23 | my $range = $self -> values_range (); | |||||||||||||||||||||||||||
| 167 | 23 | my $subsub = all_pairs (scalar $self -> evens, scalar $self -> odds); | |||||||||||||||||||||||||||
| 168 | 23 | my $subpat = "[$range]*\\g{$cell1}\\g{$cell2}[$range]*"; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 23 | map {$_ . $SENTINEL} $subsub, $subpat; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||