line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::Nonogram::BruteForce;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1548
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
7
|
use warnings;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
5
|
1
|
|
|
1
|
|
2970
|
use Storable ();
|
|
1
|
|
|
|
|
5428
|
|
|
1
|
|
|
|
|
678
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub run {
|
8
|
0
|
|
|
0
|
1
|
|
my ($class, $grid) = @_;
|
9
|
|
|
|
|
|
|
|
10
|
0
|
|
|
|
|
|
my $data = _freeze( $grid );
|
11
|
|
|
|
|
|
|
|
12
|
0
|
0
|
|
|
|
|
return if $grid->stash->{bruteforce}->{$data};
|
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
|
0
|
|
|
|
|
next if $clue->is_done;
|
21
|
0
|
0
|
0
|
|
|
|
if ( !$free or $free > $clue->{free} ) {
|
22
|
0
|
|
|
|
|
|
$free = $clue->{free};
|
23
|
0
|
|
|
|
|
|
$target = $clue;
|
24
|
|
|
|
|
|
|
}
|
25
|
|
|
|
|
|
|
}
|
26
|
0
|
0
|
|
|
|
|
return unless $target; # should be solved already
|
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
|
|
0
|
|
|
|
while( $str ne $prev and !$grid->is_done ) {
|
40
|
0
|
|
|
|
|
|
$grid->update;
|
41
|
0
|
|
|
|
|
|
$prev = $str;
|
42
|
0
|
|
|
|
|
|
$str = $grid->as_string;
|
43
|
0
|
0
|
|
|
|
|
$grid->log( $str ) if $grid->debug;
|
44
|
|
|
|
|
|
|
}
|
45
|
0
|
0
|
|
|
|
|
if ( $grid->is_done ) {
|
46
|
0
|
|
|
|
|
|
$grid->log( "Found an answer" );
|
47
|
0
|
|
0
|
|
|
|
push @{ $grid->stash->{answers} ||= [] }, $str;
|
|
0
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
}
|
49
|
|
|
|
|
|
|
else {
|
50
|
0
|
|
|
|
|
|
$class->run( $grid );
|
51
|
|
|
|
|
|
|
}
|
52
|
|
|
|
|
|
|
};
|
53
|
0
|
0
|
|
|
|
|
if ( $@ ) {
|
54
|
0
|
|
|
|
|
|
$grid->log( "Skip" );
|
55
|
|
|
|
|
|
|
}
|
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
_thaw( $grid, $data );
|
58
|
|
|
|
|
|
|
}
|
59
|
|
|
|
|
|
|
}
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _freeze {
|
62
|
0
|
|
|
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
|
|
|
|
|
|
|
sub _thaw {
|
72
|
0
|
|
|
0
|
|
|
my ($grid, $data) = @_;
|
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my @data = @{ Storable::thaw( $data ) };
|
|
0
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
1;
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
__END__
|