Last active
August 26, 2021 14:12
-
-
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
This file contains 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 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