Created
October 14, 2023 11:45
-
-
Save drench/2e60ba4752514a364ea11a9a8c14dc14 to your computer and use it in GitHub Desktop.
Blossom Cheat
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 ruby | |
# This helps suggest words for Blossom | |
# https://www.merriam-webster.com/games/blossom-word-game | |
# | |
# The script takes 2 arguments: a string of "petal" letters, and the center letter. | |
# | |
# For example, if the petals are N E O L A T and the the center is P, run: | |
# | |
# ./blossom-cheat.rb neolat p | |
# | |
# It uses the Unix dictionary for its basic word list. | |
# As you play, you will find this dictionary has words Blossom does | |
# not accept, and Blossom also accepts words not in that dictionary. | |
# | |
# Create two files: "blossom-words.txt" and "blossom-nonwords.txt". | |
# Save them in the same directory as this script. | |
# | |
# As you find exceptions, add them to the appropriate list, to make the | |
# suggestions better next time. | |
class BlossomCheat | |
MIN_WORD_LENGTH = 6 | |
def self.words_from_file(filename) | |
return [] unless File.readable?(filename) | |
File.readlines(filename).map(&:chomp) | |
end | |
attr_reader :letters, :center, :included, :excluded | |
def initialize(petals, center) | |
@letters = Set.new(petals) | |
@center = center | |
@letters.add(center) | |
@included = Set.new | |
@excluded = Set.new | |
end | |
def alls | |
Set.new(letters.reduce(anys) { |r,l| r.grep(/#{l}/) }) | |
end | |
def anys | |
regex = Regexp.new("\\A[#{letters.join("")}]+\\z") | |
Set.new(dictionary.grep(/#{center}/).grep(regex)) | |
end | |
def dictionary = included - excluded | |
def excludes(words) | |
words.each { |w| excluded.add(w) } | |
end | |
def includes(words) | |
words.each { |w| included.add(w) if w.length >= MIN_WORD_LENGTH } | |
end | |
end | |
bc = BlossomCheat.new(ARGV[0].chars, ARGV[1]) | |
bc.includes(BlossomCheat.words_from_file("/usr/share/dict/words")) | |
bc.includes(BlossomCheat.words_from_file("./blossom-words.txt")) | |
bc.excludes(BlossomCheat.words_from_file("./blossom-nonwords.txt")) | |
puts "# ALL" | |
puts bc.alls.sort.join("\n") | |
puts "\n# others" | |
puts (bc.anys - bc.alls).sort.join("\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment