Advertisement
musifter

AoC 2022, day 4 (perl 2)

Dec 4th, 2022
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.49 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.32;
  4. use warnings;
  5.  
  6. my @inside;
  7.  
  8. foreach (map { [split /\D/] } <>) {
  9.     my ($as, $ae, $bs, $be) = @$_;
  10.  
  11.     # Make sure larger range is in as-ae, smaller in bs-be
  12.     if (($ae - $as) < ($be - $bs)) {
  13.         ($as, $ae, $bs, $be) = ($bs, $be, $as, $ae);
  14.     }
  15.  
  16.     # Mark how many of b's ends are inside a's range
  17.     $inside[($as <= $bs <= $ae) + ($as <= $be <= $ae)]++;
  18. }
  19.  
  20. print "Part 1: ", $inside[2], "\n";
  21. print "Part 2: ", $inside[1] + $inside[2], "\n";
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement