Skip to content

Commit

Permalink
Merge pull request #160 from tamu-edu-students/letterboxed-refactoring
Browse files Browse the repository at this point in the history
letterboxed refactoring
  • Loading branch information
AntonioRosV authored Nov 16, 2024
2 parents 5e447e4 + 12dccd0 commit e5b7fea
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
55 changes: 31 additions & 24 deletions app/services/boxes_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@ def self.set_week_boxes

def self.set_day_box(date = Date.today)
while LetterBox.find_by(play_date: date).nil?
alphabet = ("a".."z").to_a
letters = alphabet.shuffle[0, 12]

while ([ "a", "e", "i", "o", "u" ]-letters).length > 2
letters = alphabet.shuffle[0, 12]
end
letters = get_letters()

if iterative_path_search(letters).length >= 3
LetterBox.create(letters: letters.join, play_date: date)
end
end
end

def self.iterative_path_search(letters, num_of_paths = 3)
paths = []
max_depth = 2
possible_words = {}
def self.iterative_path_search(letters, n_paths = 3)
@valid_paths = []
@max_depth = 2
@possible_words = {}
@num_of_paths = n_paths
patterns = [ letters[0..2], letters[3..5], letters[6..8], letters[9..11] ]
letters.each_with_index do |l, i|
possible_letters = letters.map(&:clone)
Expand All @@ -37,39 +33,50 @@ def self.iterative_path_search(letters, num_of_paths = 3)
words = words.select { |w| not w.match(/[#{p}][#{p}]+/) }
end

return paths if words.empty?
return @valid_paths if words.empty?

possible_words[l] = words
@possible_words[l] = words
end
while max_depth < 6 and paths.length < num_of_paths
find_paths(max_depth, num_of_paths, paths, possible_words, letters, letters)
max_depth += 1
while @max_depth < 6 and @valid_paths.length < @num_of_paths
find_paths(letters, letters)
@max_depth += 1
end

paths
@valid_paths
end

def self.find_paths(max_depth, num_of_paths, valid_paths, possible_words, remaining_letters, last_word = nil, curr_path = [], depth = 0)
if remaining_letters.empty? and not valid_paths.include? curr_path
valid_paths << curr_path
def self.find_paths(remaining_letters, last_word = nil, curr_path = [], depth = 0)
if remaining_letters.empty? and not @valid_paths.include? curr_path
@valid_paths << curr_path
end

if valid_paths.length >= num_of_paths
if @valid_paths.length >= @num_of_paths
false
else
if depth < max_depth
if depth < @max_depth
if last_word.nil?
words = possible_words.values.flatten.select { |w| not curr_path.include? w }
words = @possible_words.values.flatten.select { |w| not curr_path.include? w }
else
words = possible_words[last_word.last].select { |w| not curr_path.include? w }
words = @possible_words[last_word.last].select { |w| not curr_path.include? w }
end
words.sort_by { |w| (remaining_letters-w.chars.uniq).length }
.each do |word|
still_searching = find_paths(max_depth, num_of_paths, valid_paths, possible_words, remaining_letters - word.chars.uniq, word, curr_path+[ word ], depth + 1)
still_searching = find_paths(remaining_letters - word.chars.uniq, word, curr_path+[ word ], depth + 1)
return if not still_searching
end
end
true
end
end

def self.get_letters
alphabet = ("a".."z").to_a
letters = alphabet.shuffle[0, 12]

while ([ "a", "e", "i", "o", "u" ]-letters).length > 2
letters = alphabet.shuffle[0, 12]
end

letters
end
end
15 changes: 0 additions & 15 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions spec/services/boxes_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
RSpec.describe BoxesService, type: :service do
before do
LetterBox.destroy_all()
allow(BoxesService).to receive(:get_letters).and_return([ "i", "h", "r", "e", "w", "n", "p", "c", "s", "l", "a", "k" ])
allow(WordsService).to receive(:words_by_first_letter).with("iewnpcslak").and_return([ "ick" ])
allow(WordsService).to receive(:words_by_first_letter).with("hewnpcslak").and_return([ "hack" ])
allow(WordsService).to receive(:words_by_first_letter).with("rewnpcslak").and_return([ "ranks", "rank", "rana" ])
allow(WordsService).to receive(:words_by_first_letter).with("eihrpcslak").and_return([ "each" ])
allow(WordsService).to receive(:words_by_first_letter).with("wihrpcslak").and_return([ "wick" ])
allow(WordsService).to receive(:words_by_first_letter).with("nihrpcslak").and_return([ "nip" ])
allow(WordsService).to receive(:words_by_first_letter).with("pihrewnlak").and_return([ "pin" ])
allow(WordsService).to receive(:words_by_first_letter).with("cihrewnlak").and_return([ "can" ])
allow(WordsService).to receive(:words_by_first_letter).with("sihrewnlak").and_return([ "shawl" ])
allow(WordsService).to receive(:words_by_first_letter).with("lihrewnpcs").and_return([ "leper" ])
allow(WordsService).to receive(:words_by_first_letter).with("aihrewnpcs").and_return([ "ark" ])
allow(WordsService).to receive(:words_by_first_letter).with("kihrewnpcs").and_return([ "kicks" ])
end

describe '.set_week_boxes' do
Expand Down

0 comments on commit e5b7fea

Please sign in to comment.