Skip to content

Instantly share code, notes, and snippets.

@kamichal
Last active January 28, 2022 01:06
Show Gist options
  • Select an option

  • Save kamichal/142132b2bb2cf7d0a64b7efe0b075ad2 to your computer and use it in GitHub Desktop.

Select an option

Save kamichal/142132b2bb2cf7d0a64b7efe0b075ad2 to your computer and use it in GitHub Desktop.
Bash script for finding answer in WORDLE game
#!/usr/bin/env bash
# WORDLE game cheater
# To use it you have to install words system package
# and then source this file in bash (i.e. call `source wordle.sh`)
# This script helps to find a specific 5-letter long English words
# that match specific user's criteria.
# At the first run, it builds a complete list of words.
# Then you have 3 commands to narrow-down the list.
# 1. `include LETTERS` lefts words containing the specified LETTERS.
# 2. `exclude LETTERS` removes words containing any of the LETTERS.
# 3. `mask PATTERN` applies regex pattern to the words.
# Note that mask it works as include and also can be used for direct
# You will also need to call:
# 4. `reset` to start all over again.
# Examples:
# If you know that the solution contains `ku`:
# $ include ku
# If you know that the solution contains `r` on third position:
# $ mask ..r..
# Dots are "any character"
# If you know that the solution contains letters `w` but not on first position and `a` but not on second position:
# $ include wa
# $ mask [^w][^a]...
# When you get your answer, just call `reset`
# Enjoy
_relative() {
parent=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
echo "$parent/$1"
}
_candidates() {
candidates_file=$(_relative wordle_candidates.txt)
if [[ ! -f "$candidates_file" ]]; then
if [[ ! -d /usr/share/dict/ ]]; then
echo -e "You have to install package named words\n"
fi
grep -E "^\w{5}$" /usr/share/dict/american-english > "${candidates_file}.tmp"
grep -E "^\w{5}$" /usr/share/dict/british-english >> "${candidates_file}.tmp"
sort "${candidates_file}.tmp" | uniq > "${candidates_file}"
rm "${candidates_file}.tmp"
fi
echo "${candidates_file}"
unset candidates_file
}
_ping_pong() {
tmp="${_pp_f2:-$(_candidates)}"
_pp_f2="${_pp_f1:-$(_candidates).tmp}"
_pp_f1="${tmp}"
unset tmp
}
_ping_pong_clear() {
if [[ "${_pp_f1}" == "$(_candidates)" ]]; then
# that means there was odd number of iterations
# and thus the last operation used .tmp as a target
mv "${_pp_f2}" "${_pp_f1}"
fi
unset _pp_f1 _pp_f2
}
reset() {
rm "$(_candidates)"
}
include() {
letters="${1:?Expecting 1-5 letters!}"
for (( i=0; i<${#letters}; i++ )); do
_ping_pong
grep -i "${letters:$i:1}" "${_pp_f1}" > "${_pp_f2}"
done
_ping_pong_clear
cat "$(_candidates)"
}
exclude() {
letters="${1:?Expecting some letters!}"
for (( i=0; i<${#letters}; i++ )); do
_ping_pong
grep -iv "${letters:$i:1}" "${_pp_f1}" > "${_pp_f2}"
done
_ping_pong_clear
cat "$(_candidates)"
}
mask() {
letters="${1:?Expecting exactly 5 letters!}"
_ping_pong
grep -Ei "${letters}" "${_pp_f1}" > "${_pp_f2}"
_ping_pong_clear
cat "$(_candidates)"
}
unsource() {
# call to forget what's defined by this script
unset _relative _candidates _ping_pong _ping_pong_clear
unset include exclude mask reset unsource
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment