Spelling Bee

A solver for the NYT daily Spelling Bee, written in Raku (nee Perl 6).

A spelling be is a hex grid of characters that are to be used to produce words. The goal is to get the highest score possible by finding words, with longer words giving better scores.

bee.png

These are the spelling bee rules:

  • Each word must be at least 4 letters
  • Each word must contain the centre letter
  • Letters can be used more than once

This solver finds all the matching words from /usr/share/dict/words.

my @words = '/usr/share/dict/words'.IO.lines>>.lc;

my $centre = 't';
my @allowed = <m c i p l o t>;

for @words -> $word {
    my @chars = $word.comb;
    say $word if
        +@chars >= 4 &&             # Is at least 4 chars
        $centre (elem) @chars &&    # Contains the center char (element of set)
        @chars (<=) @allowed;       # All chars are allowed (is a subset)
}

This spelling bee produces the following words:

clipt
clit
cloit
cloot
clot
colitic
colt
colt
commit
commot
complot
coot
copilot
copt
coptic
coto
ictic
illicit
ilot
immit
implicit
impolitic
impot
itmo
licit
lilt
limit
loot
lotic
lotto
miliolitic
milt
mitotic
mitt
moit
molt
moot
motmot
mott
mott
motto
occipitootic
ocotillo
octic
octoic
octopi
omit
oolitic
optic
otic
otitic
otolitic
otomi
otto
otto
picot
pict
pict
pilot
pilot
pipit
pitpit
plot
politic
politico
polt
poot
potoo
pott
potto
ptotic
till
tillot
tilt
timo
tipiti
tiptilt
tiptop
titi
toco
toil
toit
toll
tolt
tomtit
tool
toom
toop
toot
topi
topic
topo
toto

Scoring

These are the scoring rules for the game:

  • Each 4 letter word is worth 1 point
  • Longer words earn a point for each letter
  • Pangram words that use all letters earn 7 extra points.
my @words = '/usr/share/dict/words'.IO.lines>>.lc;

sub MAIN(Str $chars, Str $centre, :$summary) {
    my @allowed = $chars.comb;

    my $final-score = 0;
    my $found-words = 0;
    my $bonus-words = 0;

    for @words -> $word {
        my @chars = $word.comb;
        if +@chars >= 4 &&          # Is at least 4 chars
        $centre (elem) @chars &&    # Contains the center char (element of set)
        @chars (<=) @allowed {      # All chars are allowed (is a subset)

            my $word-score = +@chars == 4 ?? 1 !! +@chars;
            my $remainder = @allowed (-) @chars;
            unless +$remainder {
                $word-score += 7;
                $bonus-words++;
            }

            $final-score += $word-score;
            $found-words += 1;

            my $info = +$remainder ?? "unused {$remainder.keys}" !! 'pangram';
            sprintf("%15s = %2d    %s", $word, $word-score, $info).say unless $summary;
        }
    }

    say '';
    say "Found {$found-words} words from candidate characters {$chars.subst($centre, "[$centre]")}.";
    say "This spelling bee gets {$final-score} points, with {$bonus-words} bonus words.";
    say now - ENTER now;
}
./bee.raku --summary mciplot t

Found 93 words from candidate characters mciplo[t].
This spelling bee gets 353 points, with 1 bonus words.
7.7451904
./bee.raku --summary rdighlo r
Found 53 words from candidate characters [r]dighlo.
This spelling bee gets 158 points, with 1 bonus words.
./bee.raku --summary yomahnr m
Found 168 words from candidate characters yo[m]ahnr.
This spelling bee gets 687 points, with 1 bonus words.
./bee.raku --summary hunotgd u
Found 72 words from candidate characters h[u]notgd.
This spelling bee gets 308 points, with 2 bonus words.
perl6  raku