20
1
I'm reproducing the second part of the first day of Advent of Code, with permission from the creator.
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.
An opening parenthesis, (
, means he should go up one floor, and a closing parenthesis, )
, means he should go down one floor.
The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.
Given a set of instructions, find the position of the first character that causes him to enter the basement (floor -1).
As examples:
input )
causes him to enter the basement at character position 1.
input ()())
causes him to enter the basement at character position 5.
A long input is given here that should yield the solution 1797.
This is code golf, so the shortest solution wins!
Do we have to use those exact chars? – Blue – 2016-02-17T13:12:40.703
1@muddyfish In the original challenges inputs were given in a specific form and so often a key part of the challenge was parsing the inputs; while I don't want this to become a "chameleon problem" I think the spirit of the original is that the input should be a string of brackets. I realise this does privilege some languages over others but I would call upon voters to take this into account when awarding upvotes to solutions. – A Simmons – 2016-02-17T13:16:45.330
Very closely related to Parenthifiable binary numbers ... I don't feel quite strongly enough that it's a dupe, so I'll just leave a comment instead. – AdmBorkBork – 2016-02-17T13:31:24.553
@TimmyD I see what you mean but I feel like this question is different enough that competitive answers won't be able to draw too much from that question! – A Simmons – 2016-02-17T13:38:07.223
Can we assume that he will always reach the basement at some point? – Martin Ender – 2016-02-17T13:50:47.450
Yes; on strings that cause him to never enter the basement, your program can output anything or nothing. – A Simmons – 2016-02-17T13:52:05.197
even if it's an error ? – Erwan – 2016-02-17T18:18:18.600
@Erwan yes, in the original challenge I'm reproducing, there were no malformed outputs and so you don't need to consider that case in keeping with the spirit of the puzzle. – A Simmons – 2016-02-17T18:27:09.103
1I'm trying to solve this in SMBF (basically BF), and this language SUCKS to debug... ugh. – mbomb007 – 2016-02-17T20:26:27.480