Created
April 15, 2012 12:31
-
-
Save robertmarsal/2392495 to your computer and use it in GitHub Desktop.
Problem C: Recycled Numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file = File.new("input", "r") | |
max = file.gets | |
counter = 1 | |
while (line = file.gets) | |
result = 0 | |
raw_range = line.split(" ") | |
range = raw_range[0].to_i..raw_range[1].to_i | |
computed = Hash.new | |
#discard single digit range | |
if range.max > 10 | |
range.each do |ri| | |
sri = ri.to_s.split("") | |
i = 0 | |
while i < sri.length - 1 | |
sri = sri[sri.length-1].split + sri[0, sri.length-1] | |
sri_i = sri.join.to_i | |
#no reversed | |
if sri_i != ri && range.include?(sri_i) | |
key = [ri.to_s + sri_i.to_s] | |
alter_key = [sri_i.to_s + ri.to_s] | |
computed.has_key?(alter_key) ? nil : computed[key] = true | |
end | |
i = i + 1 | |
end | |
end | |
end | |
puts "Case ##{counter}: #{computed.size}" | |
counter = counter + 1 | |
end | |
file.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment