Skip to content

Instantly share code, notes, and snippets.

@jsomers
Last active September 25, 2015 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsomers/891860 to your computer and use it in GitHub Desktop.
Save jsomers/891860 to your computer and use it in GitHub Desktop.
complex sporcle-like word puzzles
N = 6
R = 2
def masks
@masks ||= (0..N - 1).to_a.combination(N - R).to_a
end
def templates(word)
masks.inject([]) do |templates, indexes_to_hide|
arr = word.split('')
indexes_to_hide.each { |i| arr[i] = "_" }
templates << arr.join("")
end
end
words = File.open('./TWL06.txt').readlines.map(&:chomp).select { |w| w.length == N }
index = words.inject(Hash.new(0)) do |idx, word|
templates(word).each { |t| idx[t] += 1 }
idx
end
puts index.select { |_, v| v == 1 }.map(&:first)
@jsomers
Copy link
Author

jsomers commented Mar 30, 2013

Compare the method of generating all of the (6c2 * 262 = 10,140) templates first. There, for every template you'd have to check all of the words. Here you're only doing 15 ops per word.

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