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
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 ?? +@chars -3 !! 0;
              my @remainder = @allowed (-) @chars;
              $word-score += 7 && $bonus-words++ if +@remainder == 0;

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

              sprintf("%15s = %2d    unused %s", $word, $word-score, @remainder).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.";
  }
./bee.raku mciplot t
          clipt =  2    unused m o
           clit =  1    unused p o m
          cloit =  2    unused m p
          cloot =  2    unused p i m
           clot =  1    unused p i m
        colitic =  4    unused m p
           colt =  1    unused m i p
           colt =  1    unused p i m
         commit =  3    unused l p
         commot =  3    unused p i l
        complot =  4    unused i
           coot =  1    unused m i l p
        copilot =  4    unused m
           copt =  1    unused l i m
         coptic =  3    unused m l
           coto =  1    unused m i l p
          ictic =  2    unused l o p m
        illicit =  4    unused o p m
           ilot =  1    unused p c m
          immit =  2    unused p l c o
       implicit =  5    unused o
          impot =  2    unused l c
           itmo =  1    unused p c l
          licit =  2    unused m p o
           lilt =  1    unused p c o m
          limit =  2    unused p c o
           loot =  1    unused i c p m
          lotic =  2    unused m p
          lotto =  2    unused i c p m
     miliolitic =  7    unused p
           milt =  1    unused p o c
        mitotic =  4    unused l p
           mitt =  1    unused p c o l
           moit =  1    unused p l c
           molt =  1    unused p i c
           moot =  1    unused p i c l
         motmot =  3    unused p i c l
           mott =  1    unused l c i p
           mott =  1    unused p l i c
          motto =  2    unused p l i c
   occipitootic =  9    unused m l
       ocotillo =  5    unused m p
          octic =  2    unused p l m
         octoic =  3    unused l p m
         octopi =  3    unused l m
           omit =  1    unused c l p
        oolitic =  4    unused p m
          optic =  2    unused m l
           otic =  1    unused p l m
         otitic =  3    unused m l p
       otolitic =  5    unused p m
          otomi =  2    unused p c l
           otto =  1    unused p l c i m
           otto =  1    unused m p l i c
          picot =  2    unused m l
           pict =  1    unused l o m
           pict =  1    unused m o l
          pilot =  2    unused m c
          pilot =  2    unused m c
          pipit =  2    unused m o c l
         pitpit =  3    unused m c o l
           plot =  1    unused c i m
        politic =  4    unused m
       politico =  5    unused m
           polt =  1    unused m c i
           poot =  1    unused l i c m
          potoo =  2    unused m c i l
           pott =  1    unused l c i m
          potto =  2    unused m c i l
         ptotic =  3    unused m l
           till =  1    unused p c o m
         tillot =  3    unused m p c
           tilt =  1    unused p o c m
           timo =  1    unused p c l
         tipiti =  3    unused m o c l
        tiptilt =  4    unused o c m
         tiptop =  3    unused m c l
           titi =  1    unused m p o c l
           toco =  1    unused m i l p
           toil =  1    unused m c p
           toit =  1    unused m c l p
           toll =  1    unused m p c i
           tolt =  1    unused i c p m
         tomtit =  3    unused p c l
           tool =  1    unused m c i p
           toom =  1    unused p c i l
           toop =  1    unused m i c l
           toot =  1    unused m l i c p
           topi =  1    unused m l c
          topic =  2    unused l m
           topo =  1    unused m l i c
           toto =  1    unused m p c i l

Found 92 words from candidate characters mciplo[t].
This spelling bee gets 193 points, with 0 bonus words.
./bee.raku --summary rdighlo r

Found 52 words from candidate characters [r]dighlo.
This spelling bee gets 86 points, with 0 bonus words.
./bee.raku --summary yomahnr m

Found 167 words from candidate characters yo[m]ahnr.
This spelling bee gets 361 points, with 0 bonus words.
perl6  raku 
comments powered by Disqus