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:
2026-07-25 05:06:51 -04:00
parent 70ead4b520
commit e5017efde8
5 changed files with 169 additions and 5 deletions
+29 -1
View File
@@ -7,7 +7,7 @@ from collections import Counter
from .game import Clue, score_guess, str_to_pattern
from .solver import MAX_GUESSES, WORD_LEN, Solver, rank_guesses
from .wager import Wager
from .words import load_answers, load_extra_guesses
from .words import add_answer_word, load_answers, load_extra_guesses
HELP_TEXT = """\
Enter each letter followed by its result digit, e.g.:
@@ -28,6 +28,8 @@ Commands:
/round start a new wager round: clears solver + round total, exits practice
/price reset per-letter $ values to the default $1/$2/$3
/price <g> <y> <b> set per-letter $ values, e.g. `/price 1 2 3`
/addword <word> add a missing word to the answers list (and
remove it from the guesses-only list if present)
"""
@@ -96,6 +98,32 @@ def handle_slash_command(line: str, solver: Solver, wager: Wager, state: dict, a
print(f"Round total so far: ${wager.round_total} | session total: ${wager.session_total}")
return
if cmd == "/addword":
args = parts[1:]
if len(args) != 1:
print("Usage: /addword <word>")
return
word = args[0].lower()
if len(word) != WORD_LEN or not word.isalpha():
print(f"Word must be exactly {WORD_LEN} letters.")
return
if word in solver.answers:
print(f"{word.upper()} is already in the answers list.")
return
result = add_answer_word(word)
solver.answers.append(word)
solver.answers.sort()
if result["removed_from_guesses"]:
solver.extra_guesses.remove(word)
print(f"Added {word.upper()} to answers.txt and removed it from guesses.txt.")
else:
print(f"Added {word.upper()} to answers.txt.")
if all(score_guess(rec.guess, word) == rec.pattern for rec in solver.history):
solver.candidates.append(word)
return
print(f"Unknown command: {cmd} (try 'help')")
+25 -2
View File
@@ -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