Files
wordle/app.py
T

70 lines
1.6 KiB
Python

#!/usr/bin/python
from flask import Flask, render_template
import wordle
app = Flask(__name__)
def parse_guess(guess):
return "foody", "CICMI"
@app.route("/")
@app.route("/<g1>")
@app.route("/<g1>/<g2>")
@app.route("/<g1>/<g2>/<g3>")
@app.route("/<g1>/<g2>/<g3>/<g4>")
@app.route("/<g1>/<g2>/<g3>/<g4>/<g5>")
@app.route("/<g1>/<g2>/<g3>/<g4>/<g5>/<g6>")
def solver(g1=None, g2=None, g3=None, g4=None, g5=None, g6=None):
constraints = wordle.Constraints()
guesses = []
results = []
for g in (g1, g2, g3, g4, g5, g6):
parsed_guess, parsed_result = parse_guess(g)
if parsed_guess:
guesses.append(parsed_guess)
if parsed_result:
results.append(parsed_result)
if parsed_guess and parsed_result:
constraints.update_graded(parsed_guess, parsed_result)
valid_words = wordle.filter_dictionary(constraints)
scored_words = wordle.scored_words(valid_words)
# If a proposed solution was found...
if scored_words:
guesses.append(scored_words[0][0])
return render_template("solver.html", guesses=guesses, results=results)
@app.route("/dictionary")
@app.route("/dictionary/<page>")
def dictionary(page=0):
PAGE_SIZE = 100
words = sorted(wordle.dictionary())
try:
page = int(page)
except ValueError:
page = 0
max_pages = int(len(words) / PAGE_SIZE)
start_word = page * PAGE_SIZE
end_word = start_word + PAGE_SIZE
words = words[start_word:end_word]
return render_template(
"dictionary.html", page=page, max_pages=max_pages, words=words
)