Skip to content

Instantly share code, notes, and snippets.

@orodriguez
Created April 29, 2013 21:11
Show Gist options
  • Save orodriguez/92661f23482ed8daa28b to your computer and use it in GitHub Desktop.
Save orodriguez/92661f23482ed8daa28b to your computer and use it in GitHub Desktop.
class Fixnum
def palindrome?
to_s == to_s.reverse
end
end
def parse_range line
Range.new *line.split.map(&:to_i)
end
def count_palindromes file
file.reduce(0) do |total, line|
total + parse_range(line).count(&:palindrome?)
end
end
p "Found #{count_palindromes File.open("seed.txt", "r")}"
# ruby-1.9.3
# $ time ruby palindrome.rb
# "Found 106814"
# real 1m6.513s
# user 1m6.452s
# sys 0m0.020s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment