aoc2022

Advent of Code 2022 solutions in Perl.
git clone git://git.samirparikh.com/aoc2022
Log | Files | Refs | README

day04.pl (573B) - raw


      1 #!/usr/local/bin/perl
      2 # day 2022-04
      3 
      4 use strict;
      5 use warnings;
      6 use v5.32;
      7 
      8 @ARGV = "input" unless @ARGV;
      9 chomp( my $input = do { local $/; <> } );
     10 
     11 my $part1 = 0;
     12 my $part2 = 0;
     13 
     14 foreach ( split /\n/, $input ) {
     15     my ( $x1, $y1, $x2, $y2 ) = m/(\d+)-(\d+),(\d+)-(\d+)/;
     16     $part1++ if ( ( $x1 >= $x2 && $y1 <= $y2 ) ||
     17                   ( $x2 >= $x1 && $y2 <= $y1 ) );
     18     $part2++ if ( ( $y2 >= $x1 && $y2 <= $y1 ) ||
     19                   ( $x2 >= $x1 && $x2 <= $y1 ) ||
     20                   ( $x1 >= $x2 && $y1 <= $y2 ) );
     21 }
     22 
     23 say "part 1: ", $part1;
     24 say "part 2: ", $part2;