| 1 | package Games::Nonogram::BruteForce; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Storable (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||
| 8 | 0 | my ($class, $grid) = @_; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 0 | my $data = _freeze( $grid ); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | return if $grid->stash->{bruteforce}->{$data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | $grid->stash->{bruteforce}->{$data} = 1; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | $grid->log( "Start brute forcing" ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | my ($target, $free); | |||||||||||||||||||||||||||
| 19 | 0 | foreach my $clue ( $grid->clues ) { | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | next if $clue->is_done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if ( !$free or $free > $clue->{free} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $free or $free > $clue->{"free"}
MC/DC0%0/2
not $free or $free > $clue->{"free"}
not $free
$free > $clue->{"free"}
Truth table0%0/3
not(A) or B
A= not $freeB= $free > $clue->{"free"}
| |||||||||||||||||||||||||||||
| 22 | 0 | $free = $clue->{free}; | |||||||||||||||||||||||||||
| 23 | 0 | $target = $clue; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | return unless $target; # should be solved already | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | $grid->log( "target: ", $target->id ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | my @candidates = $target->candidates; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | foreach my $candidate ( @candidates ) { | |||||||||||||||||||||||||||
| 33 | 0 | $target->line->as_vec( $candidate ); | |||||||||||||||||||||||||||
| 34 | 0 | $grid->update; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | eval { | |||||||||||||||||||||||||||
| 37 | 0 | my $str = ''; | |||||||||||||||||||||||||||
| 38 | 0 | my $prev = $grid->as_string; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | while( $str ne $prev and !$grid->is_done ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$str ne $prev and not $grid->is_done
MC/DC0%0/2
$str ne $prev and not $grid->is_done
$str ne $prev
not $grid->is_done
Truth table0%0/3
A and not(B)
A= $str ne $prevB= not $grid->is_done
| |||||||||||||||||||||||||||||
| 40 | 0 | $grid->update; | |||||||||||||||||||||||||||
| 41 | 0 | $prev = $str; | |||||||||||||||||||||||||||
| 42 | 0 | $str = $grid->as_string; | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | $grid->log( $str ) if $grid->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ( $grid->is_done ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | $grid->log( "Found an answer" ); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | push @{ $grid->stash->{answers} ||= [] }, $str; | ||||||||||||||||||||||||||
|
Condition0%0/2
$grid->stash->{"answers"} ||= []
MC/DC0%0/1
$grid->stash->{"answers"} ||= []
$grid->stash->{"answers"}
Truth table0%0/2
A
A= $grid->stash->{"answers"}
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||
| 50 | 0 | $class->run( $grid ); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | }; | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $grid->log( "Skip" ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | _thaw( $grid, $data ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub _freeze { | ||||||||||||||||||||||||||
|
Subroutine
_freeze: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 62 | 0 | my $grid = shift; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my @data; | |||||||||||||||||||||||||||
| 65 | 0 | foreach my $clue ( $grid->clues ) { | |||||||||||||||||||||||||||
| 66 | 0 | push @data, $clue->line->as_vec; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 0 | return Storable::freeze( \@data ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub _thaw { | ||||||||||||||||||||||||||
|
Subroutine
_thaw: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 72 | 0 | my ($grid, $data) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my @data = @{ Storable::thaw( $data ) }; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | foreach my $clue ( $grid->clues ) { | |||||||||||||||||||||||||||
| 77 | 0 | $clue->reset_blocks; | |||||||||||||||||||||||||||
| 78 | 0 | $clue->line->as_vec( shift @data ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | 1; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | __END__ | ||||||||||||||||||||||||||||