bacek's Journal bacek's use Perl Journal en-us use Perl; is Copyright 1998-2006, Chris Nandor. Stories, comments, journals, and other submissions posted on use Perl; are Copyright their respective owners. 2012-01-25T02:46:45+00:00 pudge Technology hourly 1 1970-01-01T00:00+00:00 bacek's Journal Scripting Games, part 3 <p>Next one: <a href="">find winner of skater contest</a>.</p><p> &nbsp; &nbsp; &nbsp; &nbsp; # Calculate score for single result<br> &nbsp; &nbsp; &nbsp; &nbsp; sub calculate_score(@scores) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Sort scores<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my @sorted = sort @scores;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Drop highest and lowest<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift @sorted;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop @sorted;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Calculate final score<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ([+] @sorted)/ +@sorted;<br> &nbsp; &nbsp; &nbsp; &nbsp; };</p><p> &nbsp; &nbsp; &nbsp; &nbsp; # Parse line in form "Ken Myer,55,66,76,67,59,70,54", and return (score, name)<br> &nbsp; &nbsp; &nbsp; &nbsp; sub parse_line($line) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my @parts = $line.split(',');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reverse(shift @parts, calculate_score(@parts));<br> &nbsp; &nbsp; &nbsp; &nbsp; };</p><p> &nbsp; &nbsp; &nbsp; &nbsp; # Main cycle. Read file, parse lines, build mapping score=&gt;name<br> &nbsp; &nbsp; &nbsp; &nbsp; my $fh = open('skaters.txt') or die;<br> &nbsp; &nbsp; &nbsp; &nbsp; my %results = map &amp;parse_line, =$fh;</p><p> &nbsp; &nbsp; &nbsp; &nbsp; # Get 3 top results<br> &nbsp; &nbsp; &nbsp; &nbsp; my @top = %results.keys.sort.reverse[0..2];<br> &nbsp; &nbsp; &nbsp; &nbsp; # And show them<br> &nbsp; &nbsp; &nbsp; &nbsp; say %results{$_}, ': ', $_ for @top;</p><p>I don't know what to say. Solution is straight forward.</p> bacek 2008-12-24T07:31:03+00:00 journal Scripting Games, part 2 <p>Beginner's events are way too easy. Let's take a look at "advanced".</p><p><nobr>x<wbr></nobr> </p><p> &nbsp; &nbsp; &nbsp; &nbsp; # First thing first: build letters-to-digits map<br> &nbsp; &nbsp; &nbsp; &nbsp; my $pos = 2;<br> &nbsp; &nbsp; &nbsp; &nbsp; my %l2d;<br> &nbsp; &nbsp; &nbsp; &nbsp; map { %l2d{$^a} = $pos; %l2d{$^b} = $pos; %l2d{$^c} = $pos++ }, 'a'..'p', 'r'..'y';</p><p> &nbsp; &nbsp; &nbsp; &nbsp; # Calculate number representation of word<br> &nbsp; &nbsp; &nbsp; &nbsp; sub read_word($word) {<br> &nbsp; &nbsp; &nbsp; &nbsp; # join digit for given letters<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [~] map { %l2d{ lc $^a } }, $word.split('');<br> &nbsp; &nbsp; &nbsp; &nbsp; };</p><p> &nbsp; &nbsp; &nbsp; &nbsp; # Read wordlist and returns hash (number =&gt; word).<br> &nbsp; &nbsp; &nbsp; &nbsp; # Also skips all non-7 chars words<br> &nbsp; &nbsp; &nbsp; &nbsp; sub read_wordlist($filename) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my $fh = open($filename) or die;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map { read_word($^a) =&gt; $^a }, grep { $^a.chars == 7 }, =$fh;<br> &nbsp; &nbsp; &nbsp; &nbsp; };</p><p> &nbsp; &nbsp; &nbsp; &nbsp; # Read wordlist file.<br> &nbsp; &nbsp; &nbsp; &nbsp; my %words = read_wordlist('wordlist.txt');</p><p> &nbsp; &nbsp; &nbsp; &nbsp; # And say word corresponded to number.<br> &nbsp; &nbsp; &nbsp; &nbsp; for =$*IN {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; say uc %words{$_};<br> &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>Funny note: there is no 'scripts' in wordlist.txt<nobr> <wbr></nobr>:)</p> bacek 2008-12-24T07:01:03+00:00 journal First post. <p>Hi.</p><p>In response to Pm's <a href="">"Scripting games in Perl6"</a> I've decided to implement some of them in Perl6.</p><p>Let's start with easiest <a href=""> "2008: Beginners Event 1"</a></p><p>Task is to calculate number of different pairs of cards.</p><p> &nbsp; &nbsp; sub fact($n) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $n &lt;= 1 ?? 1 !! $n*fact($n-1);<br> &nbsp; &nbsp; }</p><p> &nbsp; &nbsp; sub calculate_pairs(@pairs) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; my %h;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Calculate number of distinct cards.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++%h{$_} for @pairs;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Sum of C^N_2<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [+] map { int(fact($^a) / 2) }, %h.values;<br> &nbsp; &nbsp; };</p><p>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.</p><p>Little trick: if number of cards in group equals to 1 than int(...) will return 0.</p><p>Update: masak++ for pointing to strange reaction on &lt;=</p> bacek 2008-12-24T06:11:24+00:00 journal