Skip to content

Instantly share code, notes, and snippets.

@jordemort
Last active May 21, 2024 16:41
Show Gist options
  • Save jordemort/45c43e23825805198615542ccba05db5 to your computer and use it in GitHub Desktop.
Save jordemort/45c43e23825805198615542ccba05db5 to your computer and use it in GitHub Desktop.
Quartiles solver
# /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)
@jordemort
Copy link
Author

Example run. Be aware that /usr/share/dict/words contains lots of "words" that Quartiles doesn't recognize:

$ python3 bin/quartiles.py
it
ine
und
nd
fru
gen
ru
de
cla
tic
bo
ape
est
ll
te
era
alt
is
spe
gr
==========
alt
alt era te
alt ru is tic
ape
bo
bo de
bo ll
bo nd
bo te
bo und
cla nd est ine
de
de alt
de gen era te
de is tic
de ll
era
era de
fru it
gen
gen era
gen era ll
gen era te
gr ape
gr ape fru it
gr is
gr it
is
it
it era te
ru de
ru ll
ru tic
spe ll
spe ll bo und
spe nd
te
te est
te ll
te nd
tic
und ine
und is te nd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment