28 lines
632 B
Ruby
28 lines
632 B
Ruby
def frequency(string)
|
|
keys = {}
|
|
keys.default = 0
|
|
for letter in string.split('') do
|
|
keys[letter] = keys[letter] + 1
|
|
end
|
|
return keys
|
|
end
|
|
|
|
def is_anagram(first, second)
|
|
return frequency(first) == frequency(second)
|
|
end
|
|
|
|
def is_unique(line)
|
|
words = line.split(' ')
|
|
for i in 0..words.length - 1 do
|
|
word = words[i]
|
|
for j in (i+1)..words.length - 1 do
|
|
if is_anagram(word, words[j]) then return false end
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
contents = File.open("puzzle_4.txt", "rb").read
|
|
lines = contents.split("\n")
|
|
puts lines.select {|line| is_unique(line)}.length
|