From cea324cad9b86f4e8cb875f9bd7e35765cb7828d Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Wed, 15 Dec 2021 11:18:48 +0000 Subject: [PATCH] Add 2017 day 9 puzzles --- 2017/puzzle-09-01.cc | 41 +++++++++++++++++++++++++++++++++++++++ 2017/puzzle-09-02.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 2017/puzzle-09-01.cc create mode 100644 2017/puzzle-09-02.cc 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; +}