aboutsummaryrefslogtreecommitdiff
path: root/challenge-005/arne-sommer/perl6/maxigrams-error
blob: d87a9c8d59dec38d3bc3b4ce84df92114bd0d34b (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
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/env perl6

unit sub MAIN (Str :$dictionary where $dictionary.IO.r = "dict-UK.txt");

my $dict = get-dictionary($dictionary);

my %count;

for $dict.keys.sort( { $^b.chars <=>  $^a.chars } ) -> $word
{
  next if $word.chars > 20;
  
  last if %count.values.max > $word.chars;
  
  %count{$word} = count-anagrams($word);
}

for %count.keys.sort( { %count{$^b} <=> %count{$^a} } )
{
  say "$_ : ", %count{$_};
}


sub get-dictionary ($file where $file.IO.r)
{
  return $file.IO.lines.grep(* !~~ /\W/)>>.lc.Set;
}

sub count-anagrams ($word)
{
  my $count = 0;
  
  $count++ if $dict{$_} for $word.comb.permutations>>.join.unique;

  say "$word: $count";
  return $count;
}