aboutsummaryrefslogtreecommitdiff
path: root/challenge-038/daniel-mita/perl6/ch-2.p6
blob: 4fc759580ef132df805df73ac6c969aafe42e928 (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
39
40
41
42
43
44
45
#!/usr/bin/env perl6

constant %values = (
  1  => 'AGISUXZ'.comb,
  2  => 'EJLRVY'.comb,
  3  => <F D P W>,
  4  => <B N>,
  5  => 'TOHMC'.comb,
  10 => <K Q>,
).invert;

constant %tiles = (
  :8A, :5B, :4C, :3D,
  :9E, :3F, :3G, :5H,
  :5I, :3J, :5K, :3L,
  :5M, :4N, :5O, :5P,
  :4Q, :3R, :7S, :3T,
  :5U, :3V, :5W, :2X,
  :5Y, :5Z,
).Bag;

#| Find the highest scoring SOWPODS word for a given number of tiles
sub MAIN (
  Int $amount where * > 0 = 7, #= Number of tiles to pick (default: 7)
  --> Nil
) {
  given %tiles.pick($amount).Bag -> %picked {
    "Tiles: %picked.kxxv.join()".say;
    # source: https://www.wordgamedictionary.com/sowpods/download/sowpods.txt
    "Winner: $_.key() for $_.value()".say with
      $?FILE.IO.parent.add('sowpods.txt').slurp.uc.words
      .grep({ .chars$amount && .comb%picked })
      .map(sub {
        given $^a => $a.comb.map({ %values{$_} }).sum {
          sprintf("%-{$amount}s: %u", |.kv).say;
          .return;
        }
      }).sort({
        given $^b.value <=> $^a.value {
          when Same { $a.key.chars <=> $b.key.chars }
          default { $_ }
        }
      }).first;
  }
}