aocpp/2015/01.cpp

36 lines
683 B
C++
Raw Permalink Normal View History

2024-06-10 11:08:30 -07:00
#include <aocpp/Startup.hpp>
#include <cstdint>
#include <stdexcept>
auto Main(std::istream & in, std::ostream & out) -> void
{
std::string line;
std::getline(in, line);
std::int64_t part1 = 0;
std::size_t part2 = 0;
for (std::size_t i = 0; i < line.size(); ++i)
{
switch (line[i]) {
case '(': part1++; break;
case ')': part1--; break;
default: throw std::runtime_error{"Invalid input character"};
}
if (part2 == 0 && part1 == -1)
{
part2 = i + 1;
}
}
if (part2 == 0)
{
throw std::runtime_error{"No part 2 solution"};
}
out << "Part 1: " << part1 << std::endl;
out << "Part 2: " << part2 << std::endl;
}