Skip to content

Instantly share code, notes, and snippets.

@nrh
Last active July 12, 2023 19:14
Show Gist options
  • Save nrh/9bed5249c4e9f7823295af6f0a1eccb9 to your computer and use it in GitHub Desktop.
Save nrh/9bed5249c4e9f7823295af6f0a1eccb9 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# program '.....' unused_letters exclusions
# board='..i..' guess=['roate','spunk','climb'] 3:a 1:s 4:m
# program '..i..' rotepunkclb 3:a 1:s 4:m
import sys
def main() -> int:
pattern = sys.argv[1]
unused_letters = sys.argv[2]
exclusions = sys.argv[3:]
for u in unused_letters:
if u in pattern:
print('user error: letter %s in \'%s\'' % (u, pattern))
return(1)
wordlist = open('/usr/share/dict/words')
for word in wordlist:
word = word.strip()
# 1. remove any words that aren't 5 letters
if len(word) != 5:
continue
# 2. remove any words that don't have letters that we know ARE used
used = ''
for x in exclusions:
used = used + x.split(':').pop()
if not matches_all_letters(word, used):
continue
# 3. remove any words that don't have a letter in the spot where we know it is
if not matches_pattern(word, pattern):
continue
# 4. remove any words that include letters that we know aren't used
if matches_letters(word, unused_letters):
continue
# 5. remove any words that have a letter in the spot where we know it's not
if matches_exclusions(word, exclusions):
continue
print(word)
def matches_exclusions(word, exclusions) -> bool:
for exclusion in exclusions:
(position,letters) = exclusion.split(':')
# position=3, letters=a
pos = int(position) - 1
for letter in letters:
if word[pos] == letter:
return True
return False
def matches_pattern(word, pattern) -> bool:
# ..i..
for i in range(0, len(pattern)):
if pattern[i] == '.':
continue
elif pattern[i] == word[i]:
continue
return False
return True
def matches_letters(word, letters) -> bool:
# rotepunkclb
for letter in letters:
if letter in word:
return True
return False
def matches_all_letters(word, letters) -> bool:
for letter in letters:
if letter not in word:
return False
return True
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment