-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboggle.rb
More file actions
38 lines (27 loc) · 706 Bytes
/
boggle.rb
File metadata and controls
38 lines (27 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require_relative 'trie'
require_relative 'board'
def search(board, pos, dict, prefix)
words = []
if dict.is_word?
words << prefix
end
board.neighbor_iter(*pos) do |x, y|
if dict.keys.member? board[x, y]
recur = search(board, [x, y], dict[board[x, y]], prefix+board[x, y])
words.concat(recur)
end
end
words
end
board = Board.new($<.readlines.map {|line| line.scan(/\w/)})
dict = Trie.new()
File.open('/usr/share/dict/words').each do |line|
dict.addword(line.chomp.downcase)
end
results = []
board.each do |x, y|
results.concat(search(board, [x, y], dict[board[x, y]], board[x, y]))
end
results.uniq!
results.sort! {|a, b| a.size - b.size}
puts results