Last active
July 9, 2026 13:55
-
-
Save instinctive/3447f611524867543c1e234d76698c18 to your computer and use it in GitHub Desktop.
Spelling Bee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env cabal | |
| {- cabal: | |
| build-depends: base, base-prelude, containers, text | |
| -} | |
| -- Spelling Bee | |
| -- | |
| -- Install Haskell: https://www.haskell.org/ghcup/ | |
| -- | |
| -- This is a standalone Haskell script, assuming you have cabal installed. The | |
| -- source dictionaries are passed on the command line, then the bee letters are | |
| -- read from stdin. The first letter is the "center". | |
| -- | |
| -- Example: $ echo "ihedtos" | ./Bee.hs /usr/share/dict/words | |
| {-# LANGUAGE NoImplicitPrelude #-} | |
| {-# LANGUAGE OverloadedStrings #-} | |
| module Main where | |
| import BasePrelude | |
| import Data.Text ( Text ) | |
| import qualified Data.Map.Strict as M | |
| import qualified Data.Set as S | |
| import qualified Data.Text as T | |
| import qualified Data.Text.IO as T | |
| type Dict = M.Map Text (S.Set Text) | |
| solve :: Dict -> Text -> [Text] | |
| solve m t = | |
| S.elems . S.unions $ lookup <$> keys | |
| where | |
| Just (c,cc) = T.uncons t | |
| keys = T.pack . sort . (c:) <$> subsequences (T.unpack cc) | |
| lookup k = fromMaybe S.empty $ M.lookup k m | |
| addword :: Dict -> Text -> Dict | |
| addword m t | |
| | T.length t < 4 = m | |
| | S.size s > 7 = m | |
| | otherwise = M.insertWith S.union k (S.singleton t) m | |
| where | |
| s = T.foldl' (flip S.insert) S.empty t | |
| k = T.pack (S.elems s) | |
| readwords :: Dict -> FilePath -> IO Dict | |
| readwords m p = addwords m <$> T.readFile p where | |
| addwords m t = foldl' addword m (T.lines t) | |
| main :: IO () | |
| main = do | |
| args <- getArgs | |
| dict <- foldM readwords M.empty args | |
| bee <- T.getLine | |
| for_ (solve dict bee) T.putStrLn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replying to the earlier question about free word games, this spelling bee game free is one I keep coming back to. As a night-shift worker who likes word games on break, the lack of daily limits is the biggest selling point for me. There's a unlimited spelling bee mode page too that walks through the basic rules for anyone just getting started.