-
Notifications
You must be signed in to change notification settings - Fork 2
/
Puzzle24.pm
56 lines (48 loc) · 1.03 KB
/
Puzzle24.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package Puzzle24;
use Math::BigRat;
use Moo;
use Scalar::Util 'reftype';
use Carp qw(croak);
use Puzzle24::Solver;
has target => (
is => 'ro',
required => 0,
);
has size => (
is => 'ro',
default => sub { 4 },
);
has pool => (
is => 'ro',
isa => sub { reftype($_[0]) eq 'ARRAY' && @{$_[0]} == 4 },
default => sub { [ (0) x $_[0]->size ] },
lazy => 1,
);
sub pool_string {
my ($self, $sep, $pre, $post) = @_;
$sep //= " ";
$pre //= "";
$post //= "";
$pre . join($sep => @{$self->pool}) . $post;
}
sub bump {
my ($self) = @_;
my ($o) = $self->pool;
my $i = $#$o;
$o->[$i--] = 0 while $o->[$i] == 9;
return if $i < 0;
$o->[$i++]++;
$o->[$i++] = $o->[$i-1] while $i < @$o;
return 1;
}
sub solver {
my ($self, $opts) = @_;
my $target = $self->target;
$opts //= {};
Puzzle24::Solver->new({ init => $self->pool,
is_winner =>
sub { Puzzle24::Solver::expr_value($_[0]) == $target },
%$opts,
});
}
1;