Skip to content

Instantly share code, notes, and snippets.

@Xophmeister
Last active August 26, 2021 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xophmeister/a2faef527a521ae7271bb75d1255422d to your computer and use it in GitHub Desktop.
Save Xophmeister/a2faef527a521ae7271bb75d1255422d to your computer and use it in GitHub Desktop.
Find valid words (not full solutions) to the New York Times' Letter Boxed game
#!/usr/bin/env bash
set -euo pipefail
readonly WORDS="${WORDS-/usr/share/dict/words}"
readonly MINIMUM_LENGTH="${MINIMUM_LENGTH-3}"
limit-alphabet() {
local alphabet="$1"
grep -E "^[${alphabet}]{${MINIMUM_LENGTH},}$"
}
remove-invalid() {
local side="$1"
local -a regex=()
local -i i
local letter
for (( i=0; i<${#side}; i++ )); do
letter="${side:${i}:1}"
regex+=("${letter}[${side}]")
done
grep -Ev "$(IFS=\|; echo "${regex[*]}")"
}
main() {
local top="$1"
local left="$2"
local bottom="$3"
local right="$4"
{
limit-alphabet "${top}${left}${bottom}${right}" \
| remove-invalid "${top}" \
| remove-invalid "${left}" \
| remove-invalid "${bottom}" \
| remove-invalid "${right}"
} \
< "${WORDS}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment