-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.pl
64 lines (56 loc) · 1 KB
/
07.pl
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
#!/usr/bin/perl -w
use strict;
use feature 'say';
use Memoize;
# Numeric sort because sort defaults to lex
# returns new array
sub nsort {
my $in = \@_;
if (@$in == 1) {
$in = $in->[0];
}
return sort {$a <=> $b} @$in;
}
sub jscore {
my $h=shift;
$h =~ tr/AKQJT/EDC1A/;
return score($h)."$h";
}
sub hscore {
my $h=shift;
$h =~ tr/AKQJT/EDCBA/;
return score($h)."$h";
}
sub score{
my %H;
my ($max,$amax) = (0);
for my $x (split('', shift)) {
$H{$x}++;
if ($H{$x} > $max && $x ne "1") {
$max = $H{$x};
$amax = $x;
}
}
if ($amax && $H{1}) {
$H{$amax} += $H{1};
delete $H{1};
}
return reverse(sprintf("%05s",join('',nsort([values %H]))));
}
sub getsum {
my $sum;
for my $i (0..$#_) {
$sum += ($i+1) * $_[$i][1];
}
return $sum;
}
memoize('score');
my @A;
while (<>) {
chomp;
push @A, [split()];
}
@A = sort {hscore($a->[0]) cmp hscore($b->[0])} @A;
say (getsum(@A));
@A = sort {jscore($a->[0]) cmp jscore($b->[0])} @A;
say (getsum(@A));