diff --git a/2017/puzzle-09-01.cc b/2017/puzzle-09-01.cc new file mode 100644 index 0000000..671cd2e --- /dev/null +++ b/2017/puzzle-09-01.cc @@ -0,0 +1,41 @@ +#include +#include + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to parse line\n"; + return 1; + } + + unsigned score{0}; + unsigned depth{0}; + bool skip{false}; + bool in_garbage{false}; + + for (auto c : line) { + if (skip) { + skip = false; + } + else if (in_garbage && c == '!') { + skip = true; + } + else if (in_garbage && c == '>') { + in_garbage = false; + } + else if (!in_garbage && c == '{') { + ++depth; + score += depth; + } + else if (!in_garbage && c == '}') { + --depth; + } + else if (!in_garbage && c == '<') { + in_garbage = true; + } + } + + std::cout << "Score = " << score << '\n'; + return 0; +} diff --git a/2017/puzzle-09-02.cc b/2017/puzzle-09-02.cc new file mode 100644 index 0000000..6401453 --- /dev/null +++ b/2017/puzzle-09-02.cc @@ -0,0 +1,46 @@ +#include +#include + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to parse line\n"; + return 1; + } + + unsigned score{0}; + unsigned garbage_count{0}; + unsigned depth{0}; + bool skip{false}; + bool in_garbage{false}; + + for (auto c : line) { + if (skip) { + skip = false; + } + else if (in_garbage && c == '!') { + skip = true; + } + else if (in_garbage && c == '>') { + in_garbage = false; + } + else if (!in_garbage && c == '{') { + ++depth; + score += depth; + } + else if (!in_garbage && c == '}') { + --depth; + } + else if (!in_garbage && c == '<') { + in_garbage = true; + } + else if (in_garbage) { + ++garbage_count; + } + } + + std::cout << "Score = " << score << '\n'; + std::cout << "Garbage count = " << garbage_count << '\n'; + return 0; +}