adventofcode/2024/08/Grid.pm

34 lines
612 B
Perl
Raw Normal View History

2024-12-10 23:34:16 +00:00
use 5.36.0;
package Grid;
use Moo;
has grid => ( is => 'ro' );
has dimensions => is => 'lazy',
default => sub ($self) {
return [ 0 + $self->{grid}->@*, 0 + $self->{grid}[0]->@* ];
};
sub get($self,$i,$j) {
return $self->grid->[$i][$j];
}
sub foreach($self,$sub) {
for my $l ( 0..$self->dimensions->[0]-1) {
for my $c ( 0..$self->dimensions->[1]-1) {
$sub->($l,$c,$self->get($l,$c));
}
}
}
sub is_inside($self,$i,$j) {
return 0 if $i < 0 or $j < 0;
return 0 if $i >= $self->dimensions->[0];
return 0 if $j >= $self->dimensions->[1];
return 1;
}
1;