Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- use List::Util qw(all);
- sub chain (&@) {
- my $block = shift;
- return (map { &$block( $_[$_-1], $_[$_] ) } (1 .. $#_));
- }
- sub check_report (@) {
- my @diffs = chain { $_[1] - $_[0] } @_;
- return (all { 1 <= abs($_) <= 3 && ($diffs[0] * $_) > 0 } @diffs);
- }
- my $part1 = 0;
- my $part2 = 0;
- REPORT:
- foreach my $report (map { [m#(\d+)#g] } <>) {
- if (&check_report( @$report )) {
- $part1++;
- next REPORT;
- }
- for (my $i = 0; $i < @$report; $i++) {
- my @new = @$report;
- splice( @new, $i, 1 );
- if (&check_report( @new )) {
- $part2++;
- next REPORT;
- }
- }
- }
- say "Part 1: ", $part1;
- say "Part 2: ", $part1 + $part2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement