| 1 | package Regexp::Sudoku::German_Whisper; | ||||||||||||||||||||||||||||
| 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 = '2022061901'; | |||||||||||||||||||||||||||
| 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 %german2cells; | |||||||||||||||||||||||||||
| 18 | 36 | fieldhash my %cell2germans; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 36 | ▶ | use List::Util qw [min max]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ################################################################################ | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # set_german_whisper ($self, @cells) | ||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | # Initialize any German whisper lines/areas | ||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||
| 29 | # TESTS: German_Whisper/100-set_german_whisper.t | ||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||
| 31 | ################################################################################ | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub set_german_whisper ($self, @cells) { | ||||||||||||||||||||||||||
|
Subroutine
set_german_whisper: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if (@cells == 1 && "ARRAY" eq ref @cells) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/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 table0%0/3
A and B
A= @cells == 1B= "ARRAY" eq ref @cells
| |||||||||||||||||||||||||||||
| 35 | 0 | @cells = @{$cells [0]} | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $name = "GW-" . (1 + keys %{$german2cells {$self} || {}}); | ||||||||||||||||||||||||||
|
Condition0%0/2
$german2cells{$self} || {}
MC/DC0%0/1
$german2cells{$self} || {}
$german2cells{$self}
Truth table0%0/2
A
A= $german2cells{$self}
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 41 | 0 | foreach my $cell (@cells) { | |||||||||||||||||||||||||||
| 42 | 0 | $cell2germans {$self} {$cell} {$name} = ++ $i; | |||||||||||||||||||||||||||
| 43 | 0 | $german2cells {$self} {$name} {$cell} = 1; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | $self; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ################################################################################ | ||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||
| 52 | # cell2renbans ($self, $cell) | ||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||
| 54 | # Return a list of German Whispers a cell belongs to. | ||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||
| 56 | # TESTS: German_Whisper/100-set_german_whisper.t | ||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||
| 58 | ################################################################################ | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 145802 | ▶ | sub cell2germans ($self, $cell) { | ||||||||||||||||||||||||||
|
Subroutine
cell2germans: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 145802 | ▶ | keys %{$cell2germans {$self} {$cell} || {}} | ||||||||||||||||||||||||||
|
Condition50%1/2
$cell2germans{$self}{$cell} || {}
MC/DC0%0/1
$cell2germans{$self}{$cell} || {}
$cell2germans{$self}{$cell}
Truth table50%1/2
A
A= $cell2germans{$self}{$cell}
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | ################################################################################ | ||||||||||||||||||||||||||||
| 66 | # | ||||||||||||||||||||||||||||
| 67 | # german2cells ($self, $cell) | ||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||
| 69 | # Return a list of cells in a German Whisper. | ||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||
| 71 | # TESTS: German_Whisper/100-set_german_whisper.t | ||||||||||||||||||||||||||||
| 72 | # | ||||||||||||||||||||||||||||
| 73 | ################################################################################ | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub german2cells ($self, $german) { | ||||||||||||||||||||||||||
|
Subroutine
german2cells: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | keys %{$german2cells {$self} {$german} || {}} | ||||||||||||||||||||||||||
|
Condition0%0/2
$german2cells{$self}{$german} || {}
MC/DC0%0/1
$german2cells{$self}{$german} || {}
$german2cells{$self}{$german}
Truth table0%0/2
A
A= $german2cells{$self}{$german}
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | ################################################################################ | ||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||
| 82 | # consecutive_in_german_whisper ($self, $cell1, $cell2) | ||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||
| 84 | # Return true, if and only if, $cell1 and $cell2 are consecutive cells | ||||||||||||||||||||||||||||
| 85 | # on a German Whispers line. | ||||||||||||||||||||||||||||
| 86 | # | ||||||||||||||||||||||||||||
| 87 | # TESTS: German_Whisper/110-consecutive_in_german_whisper.t | ||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||
| 89 | ################################################################################ | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 71877 | ▶ | sub consecutive_in_german_whisper ($self, $cell1, $cell2) { | ||||||||||||||||||||||||||
|
Subroutine
consecutive_in_german_whisper: called CC=5 SCAR=23.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | # | ||||||||||||||||||||||||||||
| 93 | # First get the German Whispers to which both cells belong. | ||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||
| 95 | 71877 | my %seen; | |||||||||||||||||||||||||||
| 96 | 71877 | $seen {$_} ++ for $self -> cell2germans ($cell1), | |||||||||||||||||||||||||||
| 97 | $self -> cell2germans ($cell2); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # | ||||||||||||||||||||||||||||
| 100 | # Now, for each German Whisper both belong, check whether they | ||||||||||||||||||||||||||||
| 101 | # are consecutive. | ||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||
| 103 | 71877 | foreach my $german (keys %seen) { | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | next unless $seen {$german} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | return 1 if abs ($cell2germans {$self} {$cell1} {$german} - | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $cell2germans {$self} {$cell2} {$german}) == 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 71877 | return 0; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | ################################################################################ | ||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||
| 114 | # make_german_statement ($self, $cell1, $cell2) | ||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||
| 116 | # Given two cell names, which are assumed to be consecutive in the same | ||||||||||||||||||||||||||||
| 117 | # German Whisper, return a sub subject and a sub pattern, which matches iff | ||||||||||||||||||||||||||||
| 118 | # the difference between the cells is at least half de size of the Sudoku. | ||||||||||||||||||||||||||||
| 119 | # | ||||||||||||||||||||||||||||
| 120 | # TESTS: German_Whisper/120-make_renban_statement.t | ||||||||||||||||||||||||||||
| 121 | # | ||||||||||||||||||||||||||||
| 122 | ################################################################################ | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub make_german_whisper_statement ($self, $cell1, $cell2) { | ||||||||||||||||||||||||||
|
Subroutine
make_german_whisper_statement: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||
| 126 | # All statements will be the same, so use "state" | ||||||||||||||||||||||||||||
| 127 | # | ||||||||||||||||||||||||||||
| 128 | 0 | state $subsub = do { | |||||||||||||||||||||||||||
| 129 | 0 | my $size = $self -> size; | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $diff = $size % 2 ? ($size + 1) / 2 : ($size / 2) + 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my @values = $self -> values; | |||||||||||||||||||||||||||
| 132 | 0 | my $subject = ""; | |||||||||||||||||||||||||||
| 133 | 0 | foreach my $i (keys @values) { | |||||||||||||||||||||||||||
| 134 | 0 | foreach my $j (keys @values) { | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | next unless abs ($i - $j) >= $diff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | $subject .= $values [$i] . $values [$j]; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | $subject . $SENTINEL | |||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $range = $self -> values_range (); | |||||||||||||||||||||||||||
| 144 | 0 | my $pair = "(?:[$range][$range])"; | |||||||||||||||||||||||||||
| 145 | 0 | my $subpat = "$pair*\\g{$cell1}\\g{$cell2}$pair*" . $SENTINEL; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | return ($subsub, $subpat); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||