Add 2017 day 9 puzzles

This commit is contained in:
2021-12-15 11:18:48 +00:00
parent 0a041cd398
commit cea324cad9
2 changed files with 87 additions and 0 deletions

41
2017/puzzle-09-01.cc Normal file
View File

@@ -0,0 +1,41 @@
#include <iostream>
#include <string>
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;
}

46
2017/puzzle-09-02.cc Normal file
View File

@@ -0,0 +1,46 @@
#include <iostream>
#include <string>
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;
}