Skip to content

Instantly share code, notes, and snippets.

@erhhung
Last active December 27, 2025 04:17
Show Gist options
  • Select an option

  • Save erhhung/2e226c50879bfa03d553f2ec355027a9 to your computer and use it in GitHub Desktop.

Select an option

Save erhhung/2e226c50879bfa03d553f2ec355027a9 to your computer and use it in GitHub Desktop.
NYT Spelling Bee
#!/usr/bin/env bash
# @describe
#
# Find words of minimum length containing one or more
# letters given, optionally requiring certain letters.
# @option -n --minimum[=3|2|4|5|6|7|8] Minimum length
# @option -r --required* Required letters
# @arg letters* Contains letters
# argument parsing: https://github.com/sigoden/argc
# words list: https://github.com/dwyl/english-words
[ "$1" ] || {
"$0" --help
exit
}
eval "$(argc --argc-eval "$0" "$@")"
#echo " Minimum: ${argc_minimum}"
#echo "Required: ${argc_required[*]}"
#echo " Letters: ${argc_letters[*]}"
hidecursor() { echo -en "\e[?25l"; }
showcursor() { echo -en "\e[?25h"; }
hidecursor
trap showcursor EXIT
# get all unique letters
unique() {
local chars="$(tr -dC '[:alpha:]' <<< "$*" | \
tr '[:upper:]' '[:lower:]')"
printf "%c\n" $(sed -E 's/(.)/ \1/g' <<< "$chars") | \
sort | uniq
}
len_cond='[ ${#word} -ge $argc_minimum ]'
#echo "len_cond: $len_cond"
unset req_cond
for l in $(unique "${argc_required[@]}"); do
[ "$req_cond" ] && req_cond+=' &&' || req_cond='[['
req_cond+=' "$word" == *'"$l"'*'
done
[ "$req_cond" ] && req_cond+=' ]]'
#echo "req_cond: $req_cond"
ltr_cond=$(printf "%c" $(unique "${argc_required[@]}" \
"${argc_letters[@]}"))
ltr_cond='[[ "$word" =~ ^['"$ltr_cond"']+$ ]]'
#echo "ltr_cond: $ltr_cond"
CR=$'\r'
while read word; do
word="${word%$CR}"
word="${word,,}"
# overwrite the current line
printf "\r\x1B[0K%s" "$word"
eval "$len_cond" || continue
eval "$req_cond" || continue
eval "$ltr_cond" && echo
done < <(sort < words.txt)
printf "\r\x1B[0K"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment