Advertisement
musifter

AoC 2024, day 1 (Perl)

Dec 1st, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.45 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(sum zip);
  8.  
  9. my (@list1, @list2, %counts);
  10.  
  11. while (<>) {
  12.     m#(\d+)\s+(\d+)#;
  13.     push( @list1, $1 );
  14.     push( @list2, $2 );
  15.     $counts{$2}++;
  16. }
  17.  
  18. @list1 = sort {$a <=> $b} @list1;
  19. @list2 = sort {$a <=> $b} @list2;
  20.  
  21. say "Part 1: ", sum map { abs($_->[0] - $_->[1]) } zip \@list1, \@list2;
  22. say "Part 2: ", sum map { $_ * ($counts{$_} // 0) } @list1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement