1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
use strict;
use warnings;
use List::Util qw(max);
sub handType {
my $hand = join("", sort(split(//, shift)));
return 7 if $hand =~ /(.)\g1{4}/;
return 6 if $hand =~ /(.)\g1{3}/;
return 5 if $hand =~ /(.)\g1{2}(.)\g2/ || $hand =~ /(.)\g1(.)\g2{2}/;
return 4 if $hand =~ /(.)\g1{2}/;
return 3 if $hand =~ /(.)\g1(.)\g2/ || $hand =~ /(.)\g1.(.)\g2/;
return 2 if $hand =~ /(.)\g1/;
return 1;
}
sub handType2 {
my $hand = shift;
max(map {
my $h = $hand;
$h =~ s/J/$_/g;
handType($h);
} qw(A K Q T 9 8 7 6 5 4 3 2));
}
sub handRanks {
my %ranks = (T => 10, J => 11, Q => 12, K => 13, A => 14);
map { $ranks{$_} // $_ } split(//, shift);
}
sub handRanks2 {
my %ranks = (T => 10, J => 1, Q => 12, K => 13, A => 14);
map { $ranks{$_} // $_ } split(//, shift);
}
sub handSort {
my $aType = handType($a);
my $bType = handType($b);
my @aRanks = handRanks($a);
my @bRanks = handRanks($b);
$aType <=> $bType || $aRanks[0] <=> $bRanks[0] ||
$aRanks[1] <=> $bRanks[1] || $aRanks[2] <=> $bRanks[2] ||
$aRanks[3] <=> $bRanks[3] || $aRanks[4] <=> $bRanks[4];
}
sub handSort2 {
my $aType = handType2($a);
my $bType = handType2($b);
my @aRanks = handRanks2($a);
my @bRanks = handRanks2($b);
$aType <=> $bType || $aRanks[0] <=> $bRanks[0] ||
$aRanks[1] <=> $bRanks[1] || $aRanks[2] <=> $bRanks[2] ||
$aRanks[3] <=> $bRanks[3] || $aRanks[4] <=> $bRanks[4];
}
my (@hands, %bids);
while (<>) {
my ($hand, $bid) = split(/ /);
push @hands, $hand;
$bids{$hand} = $bid;
}
my $winnings = 0;
my @ranked = sort handSort @hands;
for (0 .. $#ranked) {
$winnings += (1 + $_) * $bids{$ranked[$_]};
}
print $winnings, "\n";
$winnings = 0;
@ranked = sort handSort2 @hands;
for (0 .. $#ranked) {
$winnings += (1 + $_) * $bids{$ranked[$_]};
}
print $winnings, "\n";
|