1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import requests, re
BASE = 'https://www.wechall.net/challenge/lettergrid' sess = requests.Session() sess.cookies.set('WC', '...', domain='www.wechall.net')
class TrieNode: __slots__ = ('children', 'is_word') def __init__(self): self.children = {} self.is_word = False
root = TrieNode() for word in open('/usr/share/dict/words'): w = word.strip().lower() if len(w) >= 6: node = root for ch in w: node = node.children.setdefault(ch, TrieNode()) node.is_word = True
r = sess.get(f'{BASE}/generate.php') match = re.search(r'<pre>(.*?)</pre>', r.text, re.DOTALL) grid = [list(line.strip().lower()) for line in match.group(1).strip().split('\n') if line.strip()] ROWS, COLS = len(grid), len(grid[0])
DIRS = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)] found = {}
for r in range(ROWS): for c in range(COLS): for dr, dc in DIRS: node, last_word, last_len = root, None, 0 for step in range(max(ROWS, COLS)): nr, nc = r + dr*step, c + dc*step if not (0 <= nr < ROWS and 0 <= nc < COLS): break ch = grid[nr][nc] if ch not in node.children: break node = node.children[ch] if node.is_word: word = ''.join(grid[r+dr*s][c+dc*s] for s in range(step+1)) if word not in found: found[word] = (r, c)
answer = ''.join(sorted(found.keys(), key=lambda w: found[w])) print(f'Found {len(found)} words: {sorted(found.keys())}')
r = sess.get(f'{BASE}/index.php', params={'solution': answer, 'cmd': 'Submit Answer'}) if 'solved' in r.text.lower() or 'correct' in r.text.lower(): print('SOLVED!')
|