19 lines
434 B
Ruby
19 lines
434 B
Ruby
class Directions
|
|
attr_reader :steps
|
|
def initialize(steps)
|
|
@steps = steps
|
|
end
|
|
|
|
def self.for(string)
|
|
new string.split("")
|
|
end
|
|
|
|
def final_floor
|
|
steps.reduce(0) { |current_floor, step| step == "(" ? current_floor + 1 : current_floor - 1 }
|
|
end
|
|
|
|
def first_basement_step
|
|
steps.reduce([0, 0]) { |acc, step| acc[0] >= 0 ? (step == "(" ? [acc[0] + 1, acc[1] + 1] : [acc[0] - 1, acc[1] + 1]) : acc }[1]
|
|
end
|
|
end
|