Skip to content

Instantly share code, notes, and snippets.

@zemanlx
Created February 20, 2022 22:31
Show Gist options
  • Save zemanlx/ebd2832213abb38f5c9eabcd10eb9314 to your computer and use it in GitHub Desktop.
Save zemanlx/ebd2832213abb38f5c9eabcd10eb9314 to your computer and use it in GitHub Desktop.
Solving Wordle with Bash and friends: Your own AI
#!/bin/bash
# List first 10 words
head wordle.dictionary
# List only words that does not contain letters 's' and 'e'
head wordle.dictionary | grep -P "[^se]{5}"
# List only words with 'a' in the 3rd position
head wordle.dictionary | grep "..a.."
# List only words with 't' but not in the last position
head wordle.dictionary | grep t | grep '[^.][^.][^.][^.][^t]'
# Combination of all three filters on whole dictionary
grep '..a..' wordle.dictionary \
| grep t | grep '[^.][^.][^.][^.][^t]' \
| grep -P "[^se]{5}" \
| wc -l
# Build assicative array of letter weights
declare -A weights_letters
while read -r line; do
weights_letters["$(awk '{print $2}' <<< "${line}")"]="$(awk '{print $1}' <<< "${line}")"
done <<< "$(grep -o . wordle.dictionary | sort | uniq -c | sort -n)"
# Build assocative array of word weights
declare -A weights_words
while read -r word; do
weight=0
for letter in $(grep -o . <<< "${word}" | sort | uniq); do
weight=$(("${weight}" + "${weights_letters[${letter}]}"))
done
weights_words["${word}"]="${weight}"
done < wordle.dictionary
# Solver settings for words that
# - does not contain any of 'rose' letters
# - does contain 'a' but not on 1st position
sort -n <<< "$(
while read -r word; do
echo "${weights_words["${word}"]} ${word}"
done <<< "$(
grep '.....' wordle.dictionary \
| grep a \
| grep '[^a][^.][^.][^.][^.]' \
| grep -P "[^rose]{5}"
)"
)" | tail
# + 't' in the 1st position
# + 'i' not in the 2nd position
# + 'a' not in the 4th position
sort -n <<< "$(
while read -r word; do
echo "${weights_words["${word}"]} ${word}"
done <<< "$(
grep 't....' wordle.dictionary \
| grep a | grep i \
| grep '[^a][^i][^.][^a][^.]' \
| grep -P "[^rosedl]{5}"
)"
)" | tail
@zemanlx
Copy link
Author

zemanlx commented Feb 20, 2022

Visit Solving Wordle with Bash and friends: Your own AI to see the original article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment