Part 1
AoC 2015 starts simple: use a string to move up and down floors in building and the task is determining the final floor after all instructions. For this we wrote a function to count the number of floor increases/decreases, substract them to get the final floor and return that as the answer.def part_1(instructions):
return instructions.count("(") - instructions.count(")")
Testing with the supplied testcases also worked and the answer for the input was also accepted.
Part 2
For the next part we need to figure out which instruction causes our position to go negative. We track the current position by either adding or subtracting 1 to the position and return the current position once we are negative. Maybe there is a sublinear algortihm for this?
def part_2(instructions):
pos = 0
for char_pos, char in enumerate(instructions, start=1):
if char == "(":
pos += 1
if char == ")":
pos -= 1
if pos == -1:
return char_pos
return None
This was again tested against the supplied inputs and actual input.
No comments:
Post a Comment