#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say);
$/ = ''; # paragrpah mode to split sections
my ($rules, $lists) = map {[split /\n/]} <>;
$; = '|'; # hash key separator set to | (like in the input)
my %order = map { $_ => 1 } @$rules;
my @parts;
foreach my $list (@$lists) {
my @sorted = sort { $order{$a,$b} ? -1 : 1 } split( /,/, $list );
$parts[ $list eq join(',', @sorted) ] += $sorted[@sorted / 2];
}
say "Part 1: $parts[1]";
say "Part 2: $parts[0]";