Add 2021 day 10 puzzles
This commit is contained in:
40
2021/puzzle-10-01.cc
Normal file
40
2021/puzzle-10-01.cc
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
//
|
||||||
|
// Created by Matthew Gretton-Dann on 10/12/2021.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <stack>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
auto main() -> int
|
||||||
|
{
|
||||||
|
std::map<char, char> matches{{'(', ')'}, {'[', ']'}, {'{', '}'}, {'<', '>'}};
|
||||||
|
std::map<char, unsigned> scores{{')', 3}, {']', 57}, {'}', 1197}, {'>', 25137}};
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
unsigned score{0};
|
||||||
|
while (std::getline(std::cin, line)) {
|
||||||
|
std::stack<char> expecting;
|
||||||
|
for (auto c : line) {
|
||||||
|
if (auto it = matches.find(c); it != matches.end()) {
|
||||||
|
expecting.push(it->second);
|
||||||
|
}
|
||||||
|
else if (!expecting.empty()) {
|
||||||
|
if (c == expecting.top()) {
|
||||||
|
expecting.pop();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
auto score_it{scores.find(c)};
|
||||||
|
assert(score_it != scores.end());
|
||||||
|
score += score_it->second;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Total score: " << score << '\n';
|
||||||
|
return 0;
|
||||||
|
}
|
59
2021/puzzle-10-02.cc
Normal file
59
2021/puzzle-10-02.cc
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
//
|
||||||
|
// Created by Matthew Gretton-Dann on 10/12/2021.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <stack>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
auto main() -> int
|
||||||
|
{
|
||||||
|
std::map<char, char> matches{{'(', ')'}, {'[', ']'}, {'{', '}'}, {'<', '>'}};
|
||||||
|
std::map<char, unsigned> scores{{')', 1}, {']', 2}, {'}', 3}, {'>', 4}};
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
std::vector<std::uint64_t> line_scores;
|
||||||
|
while (std::getline(std::cin, line)) {
|
||||||
|
bool incomplete{true};
|
||||||
|
std::stack<char> expecting;
|
||||||
|
for (auto c : line) {
|
||||||
|
if (auto it = matches.find(c); it != matches.end()) {
|
||||||
|
expecting.push(it->second);
|
||||||
|
}
|
||||||
|
else if (!expecting.empty()) {
|
||||||
|
if (c == expecting.top()) {
|
||||||
|
expecting.pop();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
incomplete = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!incomplete) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// expecting has the characters we expect.
|
||||||
|
std::uint64_t score{0};
|
||||||
|
while (!expecting.empty()) {
|
||||||
|
score *= 5;
|
||||||
|
auto score_it{scores.find(expecting.top())};
|
||||||
|
assert(score_it != scores.end());
|
||||||
|
score += score_it->second;
|
||||||
|
expecting.pop();
|
||||||
|
}
|
||||||
|
line_scores.push_back(score);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(line_scores.begin(), line_scores.end());
|
||||||
|
std::cout << "Middle score: " << line_scores.at(line_scores.size() / 2) << '\n';
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user