Skip to content

Instantly share code, notes, and snippets.

@boone
Created December 5, 2015 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boone/8da379537a7b0f4020de to your computer and use it in GitHub Desktop.
Save boone/8da379537a7b0f4020de to your computer and use it in GitHub Desktop.
# http://adventofcode.com/day/1 - part 1
#
# instructions - String containing coded instructions for moving up a floor with
# "(" and down a floor with ")"
# Starting ground floor numbered zero is assumed.
#
# Returns an integer value for the expected floor.
def ups_and_downs(instructions)
instructions.count("(") - instructions.count(")")
end
# http://adventofcode.com/day/1 - part 2
#
# instructions - String containing coded instructions for moving up a floor with
# "(" and down a floor with ")"
# Starting ground floor numbered zero is assumed.
# Also assuming that the first character of the instructions is considered
# instruction #1 (not zero).
#
# Returns an integer value for the first instruction that leads to the basement
# (floor -1), or nil if it did not ever lead there.
def in_the_basement(instructions)
steps = instructions.split("")
floor = 0
steps.each_with_index do |step, i|
if step == "("
floor += 1
elsif step == ")"
floor -= 1
end
return i + 1 if floor == -1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment