enum HandType <HighCard OnePair TwoPair Triple FullHouse Quadruple Quintuple>;
sub hand-type($h, $part --> HandType:D) {
my %a = $h.comb.Bag;
if $part eq 'p2' && %a.keys.grep('J') && %a<J> < 5 {
my $award = %a<J>:delete;
%a{ max(%a.keys, :by({ %a{$_} })) } += $award;
}
given +%a.keys {
when 1 { HandType::Quintuple }
when 2 {
given %a.values.sort {
when [1, 4] { HandType::Quadruple }
when [2, 3] { HandType::FullHouse }
}
}
when 3 {
given %a.values.sort {
when [1, 1, 3] { HandType::Triple }
when [1, 2, 2] { HandType::TwoPair }
}
}
when 4 { HandType::OnePair }
when 5 { HandType::HighCard }
}
}
my %hands = $*IN.lines.map({ .words[0] => .words[1] });
put 'part 1: ', %hands.sort({ hand-type(.key, 'p1'), .key.trans('AKQJT' => 'FEDCB') }).map({ ++$ * .value }).sum;
put 'part 2: ', %hands.sort({ hand-type(.key, 'p2'), .key.trans('AKQJT' => 'FED1B') }).map({ ++$ * .value }).sum;