Advertisement
musifter

AoC 2024, day 2, part 1 (Perl)

Dec 1st, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.42 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7. use List::Util      qw(all);
  8.  
  9. sub chain (&@) {
  10.     my $block = shift;
  11.     return (map { &$block( $_[$_-1], $_[$_] ) } (1 .. $#_));
  12. }
  13.  
  14. my $part1 = 0;
  15. foreach my $report (map { [m#(\d+)#g] } <>) {
  16.     my @diffs = chain { $_[1] - $_[0] } @$report;
  17.     $part1++ if (all { 1 <= abs($_) <= 3 && $diffs[0] * $_ > 0 } @diffs);
  18. }
  19.  
  20. say "Part 1: $part1";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement