gets steps = gets.strip.chars current = 0 valleys = 0 in_valley = false steps.each do |dir| current += "U" == dir ? 1 : -1 if 0 == current if in_valley valleys += 1 in_valley = false end elsif 0 > current in_valley = true if in_valley == false end end puts valleys