Add 2021 day 5 puzzles

This commit is contained in:
2021-12-05 09:11:05 +00:00
parent c59a2739b6
commit 71453e98ce
2 changed files with 140 additions and 0 deletions

68
2021/puzzle-05-01.cc Normal file
View File

@@ -0,0 +1,68 @@
//
// Created by Matthew Gretton-Dann on 05/12/2021.
//
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <string>
using Point = std::pair<unsigned, unsigned>;
auto get_points(std::string const& s) -> std::pair<Point, Point>
{
static std::regex re{R"((\d+),(\d+) -> (\d+),(\d+))"};
std::smatch m;
if (!std::regex_search(s, m, re)) {
std::cerr << "Unable to interpret: " << s << "\n";
std::exit(1);
}
return {{std::stoul(m.str(1)), std::stoul(m.str(2))},
{std::stoul(m.str(3)), std::stoul(m.str(4))}};
}
auto main() -> int
{
std::string line;
std::map<Point, unsigned> vents;
while (std::getline(std::cin, line)) {
auto [pt1, pt2] = get_points(line);
std::cout << "{" << pt1.first << ", " << pt1.second << "} -> {" << pt2.first << ", "
<< pt2.second << "}: ";
if (pt1.first == pt2.first) {
unsigned y1{std::min(pt1.second, pt2.second)};
unsigned y2{std::max(pt1.second, pt2.second)};
std::cout << "vertical:\n";
while (y1 <= y2) {
auto [it, success] = vents.insert({{pt1.first, y1}, 1});
if (!success) {
++(it->second);
}
++y1;
}
}
else if (pt1.second == pt2.second) {
unsigned x1{std::min(pt1.first, pt2.first)};
unsigned x2{std::max(pt1.first, pt2.first)};
std::cout << "horizontal:\n";
while (x1 <= x2) {
auto [it, success] = vents.insert({{x1, pt1.second}, 1});
if (!success) {
++(it->second);
}
++x1;
}
}
else {
std::cout << "sloped\n";
}
}
auto danger_count{
std::accumulate(vents.begin(), vents.end(), unsigned{0},
[](auto a, auto const& it) { return a + (it.second >= 2 ? 1 : 0); })};
std::cout << "Danger spots: " << danger_count << '\n';
return 0;
}

72
2021/puzzle-05-02.cc Normal file
View File

@@ -0,0 +1,72 @@
//
// Created by Matthew Gretton-Dann on 05/12/2021.
//
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <string>
using Point = std::pair<long, long>; // NOLINT(google-runtime-int)
auto get_points(std::string const& s) -> std::pair<Point, Point>
{
static std::regex re{R"((\d+),(\d+) -> (\d+),(\d+))"};
std::smatch m;
if (!std::regex_search(s, m, re)) {
std::cerr << "Unable to interpret: " << s << "\n";
std::exit(1); // NOLINT(concurrency-mt-unsafe)
}
return {{std::stol(m.str(1)), std::stol(m.str(2))}, {std::stol(m.str(3)), std::stol(m.str(4))}};
}
auto sign(long x) -> long // NOLINT(google-runtime-int)
{
if (x == 0) {
return 0;
}
if (x > 0) {
return 1;
}
return -1;
}
auto get_delta(Point const& pt1, Point const& pt2) -> Point
{
return {sign(pt2.first - pt1.first), sign(pt2.second - pt1.second)};
}
auto main() -> int
{
std::string line;
std::map<Point, unsigned> vents;
while (std::getline(std::cin, line)) {
auto [pt1, pt2] = get_points(line);
auto delta{get_delta(pt1, pt2)};
assert(std::abs(pt1.first - pt2.first) == std::abs(pt1.second - pt2.second) ||
pt1.first == pt2.first || pt1.second == pt2.second);
std::cout << "{" << pt1.first << ", " << pt1.second << "} -> {" << pt2.first << ", "
<< pt2.second << "} - delta {" << delta.first << ", " << delta.second << "}\n";
while (pt1 != pt2) {
auto [it, success] = vents.insert({pt1, 1});
if (!success) {
++(it->second);
}
pt1.first += delta.first;
pt1.second += delta.second;
}
/* And the final point. */
auto [it, success] = vents.insert({pt1, 1});
if (!success) {
++(it->second);
}
}
auto danger_count{
std::accumulate(vents.begin(), vents.end(), unsigned{0},
[](auto a, auto const& it) { return a + (it.second >= 2 ? 1 : 0); })};
std::cout << "Danger spots: " << danger_count << '\n';
return 0;
}