Last active
May 21, 2024 16:41
-
-
Save jordemort/45c43e23825805198615542ccba05db5 to your computer and use it in GitHub Desktop.
Quartiles solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /usr/bin/env python3 | |
""" | |
A solver for the Quartiles game in Apple News | |
Input your tiles separated by spaces or newlines. This program will find | |
all entries in /usr/share/dict/words that match a permutation of 1, 2, 3, | |
or 4 tiles. | |
""" | |
import fileinput | |
from itertools import permutations | |
fragments: set[str] = set() | |
words: set[str] = set() | |
for line in fileinput.input(): | |
for fragment in line.strip().lower().split(): | |
fragments.add(fragment) | |
with open("/usr/share/dict/words", "r") as words_in: | |
for line in words_in: | |
word = line.strip() | |
if word.lower() != word: | |
continue | |
words.add(word) | |
print("==========") | |
found: set[str] = set(filter(lambda word: word in words, fragments)) | |
for i in range(2, 5): | |
found.update( | |
map( | |
lambda x: " ".join(x), | |
filter( | |
lambda x: "".join(x) in words, | |
permutations(fragments, i), | |
), | |
) | |
) | |
for found_word in sorted(found): | |
print(found_word) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example run. Be aware that
/usr/share/dict/words
contains lots of "words" that Quartiles doesn't recognize: