r/programming • • Dec 01 '15

Daily programming puzzles at Advent of Code

http://adventofcode.com/
320 Upvotes

179 comments sorted by

View all comments

1

u/OwlsParliament Dec 01 '15 edited Dec 01 '15

My C++ solution - probably overly verbose, but looping over a string is simpler than recursively doing it in C++.

    #include <iostream>
#include <string>

int stairs(const std::string& brackets)
{
	int result = 0;
	for(size_t position = 0; position < brackets.size(); ++position)
	{
		if(result == -1)
		{
			std::cout << "Entered basement at: " << position << std::endl;
		}
		(brackets[position] == '(') ? result++ : result--;
	}
	return result;
}

int main()
{
	std::string brackets;
	std::cin >> brackets;
	int floor = stairs(brackets);
	std::cout << "Santa goes to floor: " << floor << std::endl;
	
	return 0;
}