Advertisement
musifter

AoC 2022, day 4 (Perl)

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