adventofcode/2020/03/part2.pl

47 lines
634 B
Perl
Raw Normal View History

2021-12-05 16:51:21 +00:00
use 5.20.0;
use warnings;
use experimental qw/
signatures
postderef
/;
use Path::Tiny;
my @forest = path(shift)->lines({chomp => 1 });
sub go_down($forest,$right,$down=1) {
my $trees = 0;
my @forest = @$forest;
my $line = shift @forest;
my $i = 0;
use DDP;
while( $line) {
$trees++ if '#' eq substr $line, $i, 1;
$i+=$right;
$i %= length $line;
$line = shift @forest for 1..$down;
}
return $trees
}
use List::AllUtils qw/ product /;
$, = " ";
print product map { go_down([@forest],@$_) } (
[1,1],
[3,1],
[5,1],
[7,1],
[1,2],
)