-
-
Save benjacoblee/84eb2bf4733a92d7910bd3aadff3c2c2 to your computer and use it in GitHub Desktop.
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
import std/[strutils, sequtils, parseopt, tables] | |
let f = readFile("valid-wordle-words.txt") | |
var | |
p = initOptParser() | |
tbl = initTable[string, int]() | |
charsInWrongSpot: seq[string] = @[] | |
charsNotInWord: seq[string] = @[] | |
try: | |
while true: | |
p.next() | |
case p.kind | |
of cmdEnd: | |
break | |
of cmdArgument: | |
discard | |
of cmdShortOption: | |
tbl[p.key] = parseInt(p.val) | |
of cmdLongOption: | |
if not(p.val.len == 1 and LowercaseLetters.contains(p.val[0])): | |
raise new ValueError | |
if p.key == "not": | |
charsNotInWord.add(p.val.toLower()) | |
elif p.key == "opt": | |
charsInWrongSpot.add(p.val.toLower()) | |
except: | |
echo "Error: could not parse options due to ", getCurrentException().name | |
quit(0) | |
proc makeGuessStr(): string = | |
var strSeq = newSeq[string](5) | |
for k, v in tbl.pairs: | |
strSeq[v - 1] = k | |
return strSeq.map(proc (str: string): string = | |
if str == "": result = "-" | |
else: result = str | |
).join("") | |
proc main() = | |
let | |
guess = makeGuessStr() | |
words = f.splitLines() | |
var wordsArr: seq[string] = @[] | |
for word in words: | |
var match = true | |
for i in 0 ..< guess.len: | |
if guess[i] != '-': | |
if guess[i] != word[i]: | |
match = false | |
break | |
if match: | |
wordsArr.add(word) | |
for ch in charsInWrongSpot: | |
wordsArr = wordsArr.filterIt(it.contains(ch)) | |
for ch in charsNotInWord: | |
wordsArr = wordsArr.filterIt(not it.contains(ch)) | |
echo wordsArr | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment