Advertisement
musifter

AoC 2024, day 5 (Perl)

Dec 5th, 2024 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.51 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7.  
  8. $/ = '';        # paragrpah mode to split sections
  9. my ($rules, $lists) = map {[split /\n/]} <>;
  10.  
  11. $; = '|';       # hash key separator set to | (like in the input)
  12. my %order = map { $_ => 1 } @$rules;
  13.  
  14. my @parts;
  15. foreach my $list (@$lists) {
  16.     my @sorted = sort { $order{$a,$b} ? -1 : 1 } split( /,/, $list );
  17.     $parts[ $list eq join(',', @sorted) ] += $sorted[@sorted / 2];
  18. }
  19.  
  20. say "Part 1: $parts[1]";
  21. say "Part 2: $parts[0]";
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement