Add /addword command to add missing Wordle answers
Some real Wordle answers aren't in the bundled answers.txt list. This adds a REPL command to append a word to answers.txt and remove it from guesses.txt if present there, since the two lists are meant to be disjoint. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+25
-2
@@ -10,8 +10,8 @@ from pathlib import Path
|
||||
DATA_DIR = Path(__file__).parent / "data"
|
||||
|
||||
|
||||
def _load(filename: str) -> list:
|
||||
path = DATA_DIR / filename
|
||||
def _load(filename: str, data_dir: Path = DATA_DIR) -> list:
|
||||
path = data_dir / filename
|
||||
return [w.strip().lower() for w in path.read_text().splitlines() if w.strip()]
|
||||
|
||||
|
||||
@@ -25,3 +25,26 @@ def load_extra_guesses() -> list:
|
||||
|
||||
def load_all_valid_words() -> list:
|
||||
return load_answers() + load_extra_guesses()
|
||||
|
||||
|
||||
def add_answer_word(word: str, data_dir: Path = DATA_DIR) -> dict:
|
||||
"""Add `word` to the bundled answers list, removing it from the extra
|
||||
guesses list if it's there (the two lists are meant to be disjoint)."""
|
||||
word = word.strip().lower()
|
||||
answers = _load("answers.txt", data_dir)
|
||||
guesses = _load("guesses.txt", data_dir)
|
||||
|
||||
result = {"added": False, "removed_from_guesses": False}
|
||||
|
||||
if word not in answers:
|
||||
answers.append(word)
|
||||
answers.sort()
|
||||
(data_dir / "answers.txt").write_text("\n".join(answers) + "\n")
|
||||
result["added"] = True
|
||||
|
||||
if word in guesses:
|
||||
guesses = [w for w in guesses if w != word]
|
||||
(data_dir / "guesses.txt").write_text("\n".join(guesses) + "\n")
|
||||
result["removed_from_guesses"] = True
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user