Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add my (quite C-like and densely-packed) solution to the lock problem. #22

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions solutions/jre21/lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import re

lock = ['BDMJPRSTLN', 'AEIOUYRTLH', 'ACDEORSTLN', 'DHKYRSTLNE']

words = set()

# Create a hash table of all four-letter words which use only standard
# (unaccented) characters.
with open('/usr/share/dict/words', 'r') as f:
word = f.readline()
while (word):
word = word.strip().upper()
if re.match('^[A-Z]{4}$', word):
words.add(word)
word = f.readline()

solutions = []

# Iterate through all possible positions of the bottom three reels
# relative to the top reel. Then, for each position, enumerate the
# columns which form real words. Save all sets of 3+ words.
for second in range(len(lock[1])):
for third in range(len(lock[2])):
for fourth in range(len(lock[3])):
found = []
for index in range(len(lock[0])):
word = ''.join([
lock[0][index],
lock[1][(second+index)%len(lock[1])],
lock[2][(third+index)%len(lock[2])],
lock[3][(fourth+index)%len(lock[3])]
])
if word in words:
found.append(word)
if len(found) >= 3:
found.sort()
solutions.append(tuple(found))

# Print the solutions, alphabetized by first word.
solutions = list(set(solutions))
solutions.sort(key=lambda x: x[0])
for solution in solutions:
print(solution)

# Print in alphabetical order all words which appear in a triplet.
answers = list(set([x for y in solutions for x in y]))
answers.sort()
print(answers)