Skip to content

Instantly share code, notes, and snippets.

@AnimaWish
Created February 26, 2013 06:28
Show Gist options
  • Save AnimaWish/5036367 to your computer and use it in GitHub Desktop.
Save AnimaWish/5036367 to your computer and use it in GitHub Desktop.
#TO DO: import puzzle file, puzzle generator, solutions view omitting unused characters, solutions view highlighting selected words, verbose options, test multiple solutions
samplepuzzle = [ ['b', 'b', 'c', 'd', 'e'],
['f', 'e', 'g', 'g', 'j'],
['k', 'a', 'l', 'o', 'o'],
['s', 'r', 's', 'l', 's'],
['u', 's', 'w', 'd', 'y']]
samplebank = [%w{b e a r s}, %w{e g g}, %w{g o l d}, %w{b e l l}]
puzzle = Array.new
puts "Input puzzle line by line (separate puzzle lines by newline). Omit any whitespace. Signify end of puzzle with '***' on newline."
loop do
puzzleline = gets.chomp.downcase
if puzzleline == '***'
break
end
puzzle << puzzleline.split('')
if puzzleline == '/'
puzzle = samplepuzzle
break
end
end
bank = Array.new
puts "Input wordbank word by word (separate words by newline). Omit any whitespace. Signify end of puzzle with '***' on newline."
loop do
bankline = gets.chomp.downcase
if bankline == '***'
break
end
bank << bankline.split('')
if bankline == '/'
bank = samplebank
break
end
end
# puts "Input options unseparated by whitespace. h for help, a for all."
# options = gets.chomp
# case options
solutions = Hash.new
hash_positions = Hash.new #_asdf refers to the key
hash_letters = Hash.new
('a'..'z').each do |letter| #this area indexes all the letters and sticks 'em into the hash
row = 0
locarray = Array.new
puzzle.each do |x|
xindexes = x.flat_map.with_index { |item,index| item == letter ? [index] : [] } #this is literal magic pulled from IRC. if item matches letter, stick index into an array created by flat_map
xindexes.each do |y|
unless y == nil
locarray << [row, y]
end
end
row += 1
end
locarray.each {|x| hash_positions[x] = letter}
hash_letters[letter] = locarray
end
character = 0
(0..bank.length-1).each do |word|
(0..hash_letters[bank[word][character]].length-1).each do |hashvalue|
ain = hash_letters[bank[word][character]][hashvalue] #look inhash_letters for the first value in the array that corresponds to the first letter of the first value in the wordbank
[-1, 0, 1].map do |x|
[-1, 0, 1].map do |y|
x2 = ain[0]+x; y2 = ain[1]+y #turns directions in last 2 lines into positions
if hash_positions[[x2,y2]] == bank[word][character+1] && [x,y] != [0,0] #this is where the magic happens
#p [[x,y],[x2,y2]]
assembledword = [bank[word][character], hash_positions[[x2,y2]]] #creates an array with the first two letters supplied
assembledwordlocations = [ain, [x2,y2]] #creates an array with the first two locations supplied
assembledwordcounter = 1
(bank[word].length-2).times do #sticks next word.length-2 characters in the discovered direction into assembledx variables for comparison
assembledword << hash_positions[[x2 + (x*assembledwordcounter),(y2 + y*assembledwordcounter)]]
assembledwordlocations << [x2 + x*assembledwordcounter, y2 + y*assembledwordcounter]
assembledwordcounter += 1
end
p assembledword
p assembledwordlocations
if assembledword == bank[word]
puts "Success"
solutions[assembledword] = assembledwordlocations
end
puts
end
end
end
end
end
puts
puts; p hash_positions; puts; p hash_letters; puts; p bank; puts; p solutions
puts "\n__PUZZLE SOLUTION__"
solutions.each do |x|
puts x[0].join + "\t" + x[1].to_s
end
puts
puts "Input view options. h for help. q to quit."
loop do
option = gets.chomp.downcase
if option == 'h'
puts 'a - all view options (except highlight word)'
puts 'n - view unchanged puzzle'
puts 'l - highlight word (input word on newline, loops. \'***\' to exit.)'
puts 'u - show only letters used in solutions'
else
case option
when 'a'
puts 'all'
when 'n'
puzzle.each do |x|
puts "%s "*x.length % x
end
when 'l'
puts 'highlight view'
highlightword = gets.chomp.downcase.split('')
puts highlightword.join + "\t" + solutions[highlightword].to_s
viewpuzzle = Array.new #this nasty code creates a copy of puzzle inside viewpuzzle
puzzle.each do |x|
viewpuzzleline = Array.new
x.each do |y|
viewpuzzleline << y
end
viewpuzzle << viewpuzzleline
end
# viewpuzzle.each do |x| #COMBINE THIS BLOCK WITH LINE 163
# x.each do |y|
# p hash_positions.key(y)
# p [viewpuzzle.index(x), x.index(y)]
# # puts x.to_s + y.to_s + viewpuzzle.index(x).to_s
# unless solutions[highlightword].include? [viewpuzzle.index(x), x.index(y)]
# viewpuzzle[viewpuzzle.index(x)][x.index(y)] = '.'
# end
# end
# end
puzzle.each do |x|
#p solutions[highlightword]
x.each do |y|
unless solutions[highlightword].include? [puzzle.index(x), x.index(y)]
#p [puzzle.index(x), x.index(y)]
print '. '
else
print y + ' '
end
end
print "\n"
end
# p solutions[highlightword]
# viewpuzzle.each do |x|
# puts "%s "*x.length % x
# end
when 'u'
puts 'used letters view'
when 'q'
break
end
end
end
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment