adventofcode/2024/02/Part1.pm

33 lines
565 B
Perl
Raw Permalink Normal View History

2024-12-04 20:46:05 +00:00
package Day2::Part1;
use 5.36.0;
use List::Util qw/ sum pairmap /;
use List::MoreUtils qw/ part zip /;
sub read_file(@lines) {
return map { [split] } @lines;
}
sub is_safe(@report) {
my $asc = $report[1] - $report[0];
my $level = shift @report;
while(@report) {
my $next = shift @report;
my $delta = $next - $level;
return 0 unless $delta*$asc >= 0;
return 0 unless 1 <= abs($delta) <= 3;
$level = $next;
}
return 1;
}
sub solve(@reports) {
return sum map { is_safe(@$_) } @reports;
}
1;