Next one: find winner of skater contest.
# Calculate score for single result
sub calculate_score(@scores) {
# Sort scores
my @sorted = sort @scores;
# Drop highest and lowest
shift @sorted;
pop @sorted;
# Calculate final score
([+] @sorted)/ +@sorted;
};
# Parse line in form "Ken Myer,55,66,76,67,59,70,54", and return (score, name)
sub parse_line($line) {
my @parts = $line.split(',');
reverse(shift @parts, calculate_score(@parts));
};
# Main cycle. Read file, parse lines, build mapping score=>name
my $fh = open('skaters.txt') or die;
my %results = map &parse_line, =$fh;
# Get 3 top results
my @top = %results.keys.sort.reverse[0..2];
# And show them
say %results{$_}, ': ', $_ for @top;
I don't know what to say. Solution is straight forward.
Beginner's events are way too easy. Let's take a look at "advanced".
http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent1.msp
# First thing first: build letters-to-digits map
my $pos = 2;
my %l2d;
map { %l2d{$^a} = $pos; %l2d{$^b} = $pos; %l2d{$^c} = $pos++ }, 'a'..'p', 'r'..'y';
# Calculate number representation of word
sub read_word($word) {
# join digit for given letters
[~] map { %l2d{ lc $^a } }, $word.split('');
};
# Read wordlist and returns hash (number => word).
# Also skips all non-7 chars words
sub read_wordlist($filename) {
my $fh = open($filename) or die;
map { read_word($^a) => $^a }, grep { $^a.chars == 7 }, =$fh;
};
# Read wordlist file.
my %words = read_wordlist('wordlist.txt');
# And say word corresponded to number.
for =$*IN {
say uc %words{$_};
}
Funny note: there is no 'scripts' in wordlist.txt
Hi.
In response to Pm's "Scripting games in Perl6" I've decided to implement some of them in Perl6.
Let's start with easiest "2008: Beginners Event 1"
Task is to calculate number of different pairs of cards.
sub fact($n) {
$n <= 1 ?? 1 !! $n*fact($n-1);
}
sub calculate_pairs(@pairs) {
my %h;
# Calculate number of distinct cards.
++%h{$_} for @pairs;
# Sum of C^N_2
[+] map { int(fact($^a) / 2) }, %h.values;
};
Trivial mathematical solution: number of different pairs for given amount of cards is C^n_2. So, I group cards, calculate number of combinations in different group and sum them.
Little trick: if number of cards in group equals to 1 than int(...) will return 0.
Update: masak++ for pointing to use.perl.org strange reaction on <=