Skip to content

Commit

Permalink
Problem 112
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbantuelle committed Oct 5, 2018
1 parent 3c66a74 commit 2b707dc
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 112.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'pp'

def bouncy(n)
digits = n.to_s.chars.map(&:to_i)
return false if digits.length == 1
if digits[0] < digits[1]
directionality = 'ascending'
elsif digits[0] == digits[1]
directionality = 'equal'
else
directionality = 'descending'
end
1.upto(digits.length-2) do |i|
if directionality == 'equal'
if digits[i] < digits[i+1]
directionality = 'ascending'
elsif digits[i] > digits[i+1]
directionality = 'descending'
end
else
return true if (directionality == 'ascending' && digits[i] > digits[i+1]) || (directionality == 'descending' && digits[i] < digits[i+1])
end
end
false
end

count = 0
bouncy = 0
bouncy_ratio = 0
while bouncy_ratio != 0.99
count += 1
bouncy += 1 if bouncy(count)
bouncy_ratio = bouncy.to_f/count
end

pp count

0 comments on commit 2b707dc

Please sign in to comment.