Created
December 5, 2015 19:13
-
-
Save boone/8da379537a7b0f4020de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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