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;

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

  my $final-score = 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;
         $word-score += 7 && $bonus-words++ if +@remainder == 0;

         $final-score += $word-score;

         sprintf("%15s = %2d    unused %s", $word, $word-score, @remainder).say;
      }
  }

  say '';
  say "This spelling bee gets {$final-score} points, with {$bonus-words} bonus words.";
          clipt =  5    unused m o
           clit =  1    unused m o p
          cloit =  5    unused p m
          cloot =  5    unused m i p
           clot =  1    unused p i m
        colitic =  7    unused m p
           colt =  1    unused m i p
           colt =  1    unused m i p
         commit =  6    unused p l
         commot =  6    unused i l p
        complot =  7    unused i
           coot =  1    unused i m p l
        copilot =  7    unused m
           copt =  1    unused i m l
         coptic =  6    unused l m
           coto =  1    unused p l i m
          ictic =  5    unused p l o m
        illicit =  7    unused o p m
           ilot =  1    unused m c p
          immit =  5    unused p o l c
       implicit =  8    unused o
          impot =  5    unused l c
           itmo =  1    unused l p c
          licit =  5    unused m o p
           lilt =  1    unused p o c m
          limit =  5    unused p o c
           loot =  1    unused m c i p
          lotic =  5    unused p m
          lotto =  5    unused c i m p
     miliolitic = 10    unused p
           milt =  1    unused c o p
        mitotic =  7    unused l p
           mitt =  1    unused c p o l
           moit =  1    unused c l p
           molt =  1    unused p i c
           moot =  1    unused i c l p
         motmot =  6    unused p l i c
           mott =  1    unused c i p l
           mott =  1    unused i c p l
          motto =  5    unused p l c i
   occipitootic = 12    unused l m
       ocotillo =  8    unused p m
          octic =  5    unused l p m
         octoic =  6    unused p l m
         octopi =  6    unused m l
           omit =  1    unused c l p
        oolitic =  7    unused m p
          optic =  5    unused l m
           otic =  1    unused l p m
         otitic =  6    unused m l p
       otolitic =  8    unused m p
          otomi =  5    unused p l c
           otto =  1    unused i c m p l
           otto =  1    unused m c i l p
          picot =  5    unused l m
           pict =  1    unused m o l
           pict =  1    unused m o l
          pilot =  5    unused c m
          pilot =  5    unused c m
          pipit =  5    unused c m l o
         pitpit =  6    unused c m l o
           plot =  1    unused m c i
        politic =  7    unused m
       politico =  8    unused m
           polt =  1    unused c i m
           poot =  1    unused m c i l
          potoo =  5    unused l m i c
           pott =  1    unused l m i c
          potto =  5    unused i c m l
         ptotic =  6    unused m l
           till =  1    unused p o c m
         tillot =  6    unused c m p
           tilt =  1    unused m c o p
           timo =  1    unused c l p
         tipiti =  6    unused m c o l
        tiptilt =  7    unused o m c
         tiptop =  6    unused l m c
           titi =  1    unused m c l o p
           toco =  1    unused i m p l
           toil =  1    unused m c p
           toit =  1    unused c m p l
           toll =  1    unused i c m p
           tolt =  1    unused m c i p
         tomtit =  6    unused p l c
           tool =  1    unused c i m p
           toom =  1    unused p l i c
           toop =  1    unused c i m l
           toot =  1    unused m i c l p
           topi =  1    unused l c m
          topic =  5    unused m l
           topo =  1    unused i c m l
           toto =  1    unused m c i l p

This spelling bee gets 337 points, with 0 bonus words.
perl6  raku 
comments powered by Disqus