跳到正文
DEV Community· venn·· 57 分鐘前AI 評分31

打造 Poople 解題器:搜尋容易,詞表才是難題

Building a Poople Solver: The Search Was Easy, the Word List Was Not

AI 導讀

作者為每日四字母文字遊戲 Poople 製作解題器,從 POOP 執行廣度優先搜尋,找出起始詞到終點的最短路徑。使用 2,303 個詞的詞表時,有 2,166 個詞可連通 POOP;逐日補入遊戲接受但詞表遺漏的詞,可提高解題準確度。解題器在瀏覽器內運行,不會把用戶輸入的詞傳送至伺服器。

正文

Poople is a daily word puzzle. You get a four-letter start word and change one letter at a time until you reach POOP. Every guess has to be a real word, and fewer steps is better.

I built a Poople solver that finds the shortest path from any start word. The search code is short. Getting the word list right was the hard part.

Words as a map

Think of every four-letter word as a stop on a map. Two words are connected when they differ by exactly one letter in the same spot. JUGS connects to BUGS. BUGS connects to BOGS.

Solving a puzzle means finding the shortest route on this map to POOP.

Start from the finish line

Every puzzle ends at POOP. So instead of searching from each start word, I search once, outward from POOP. First all words one step away, then all words two steps away, and so on. This is called a breadth-first search.

When it finishes, I know how far every word is from POOP. Solving any start word is then just a lookup.

from collections import deque

def neighbors(word, words):
    for i in range(4):
        for c in "abcdefghijklmnopqrstuvwxyz":
            cand = word[:i] + c + word[i + 1:]
            if cand != word and cand in words:
                yield cand

def distances(words, target="poop"):
    dist = {target: 0}
    queue = deque([target])
    while queue:
        w = queue.popleft()
        for n in neighbors(w, words):
            if n not in dist:
                dist[n] = dist[w] + 1
                queue.append(n)
    return dist

To get the actual path, start at your word and keep stepping to any neighbor that is one step closer to POOP.

When there is more than one shortest path

JUGS (puzzle #464) has three 5-step paths:

  • JUGS → BUGS → BOGS → BOOS → BOOP → POOP
  • JUGS → JOGS → COGS → COOS → COOP → POOP
  • JUGS → JOGS → BOGS → BOOS → BOOP → POOP

All three score the same, but people find some easier to follow than others. So for each path I look at its least common word. The more familiar that word is, the higher the path goes in the list. The word list is sorted by how often each word shows up in movie subtitles, which works well as a "would a normal person know this word" score.

The hard part: which words does the game accept?

The code above only works if the word list matches the game. That turned out to be the real problem.

A big list is too loose. ENABLE, a free word list used by many word games, has 3,903 four-letter words. Many of them are words almost nobody uses. When I tried some in the game, it rejected AITS, ANTA, ARES and SIMS.

A small list is too strict. So I kept only the 2,303 words that are in both ENABLE and a standard-size English dictionary. That removed the junk, but it also removed words the game does accept.

Puzzle #465 showed the problem. The start word was ANTI. My solver said the best path was 10 steps. The game said 8.

Playing it in the game found the gap: it accepts ANDS and BODS, and my list had dropped both. With those two words back in, the solver finds the same 8-step path:

ANTI → ANTS → ANDS → AIDS → BIDS → BODS → BOOS → BOOP → POOP

The next day the game showed this exact path as its own answer.

Two small files fix most of it

There is no public list of the words Poople accepts, so I keep two plain text files:

  • added.txt: words the game accepted that my list missed (BOOP, ANDS, BODS so far)
  • rejected.txt: words the game turned down (AITS, ANTA, ARES, SIMS)

Each day I compare my answer with the game's best score. If the game wins, I find the missing word and add it. Each fix makes the next day a bit more accurate.

A few numbers from the map

Using my list of 2,303 words:

  • 2,166 words can reach POOP. The other 136 have no path at all.
  • About 4 in 5 reachable words need 4 to 6 steps.
  • ANTI needed 8 steps, more than 96% of possible start words.
  • The farthest word is OBIT, at 12 steps.

Runs in the browser

2,300 short words is a small file, so the whole solver runs in the page. Nothing is sent to a server, and the word you type never leaves your device.

That also made a hint mode easy. "Next step only" shows just the next word and how many steps are left, for people who want a nudge rather than the full answer.

Try it

The Poople solver is free and needs no sign-up. If you want to know how we check each day's answer against the game, it is written up in How we work out Poople answers.

If you have built something similar, I would like to hear how you handled the word list.

This post was written with help from AI.

來源:DEV Community · dev.to