Scripting Games, part 2

bacek on 2008-12-24T07:01:03

Beginner's events are way too easy. Let's take a look at "advanced".

http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent1.mspx

# 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 :)