summaryrefslogtreecommitdiff
path: root/noncore/games/wordgame/calcdist
blob: faf31f10fc0020555f34de07938393f487449702 (plain)
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
#!/usr/bin/perl

# Usage:  cat dictionaries | grep -v '[^a-z]' | calcdist n score
#
#   Given a lot of words, find an appropriate distribution
#   into n tiles with tile values proportional to the square root
#   of the ratio of score to the tile's frequency.

$n = shift;
$score = shift;

while (<>) {
    chomp;
    for $c ( split "", $_ ) {
	$freq{$c}++;
	$t++;
    }
}

for $c ( sort { $freq{$a} <=> $freq{$b} } keys %freq ) {
    #print "$c: $freq{$c}\n";
    $need = int($freq{$c}*$n/$t+0.5) || 1;
    $value = int(sqrt($score/($freq{$c}*$n/$t))+0.5) || 1;
    $t -= $freq{$c};
    $n -= $need;
    print "$need $c $value\n";
}