From 71453e98ce0f7c19b8f5a3d55b2f699744623dd6 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Sun, 5 Dec 2021 09:11:05 +0000 Subject: [PATCH] Add 2021 day 5 puzzles --- 2021/puzzle-05-01.cc | 68 +++++++++++++++++++++++++++++++++++++++++ 2021/puzzle-05-02.cc | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 2021/puzzle-05-01.cc create mode 100644 2021/puzzle-05-02.cc diff --git a/2021/puzzle-05-01.cc b/2021/puzzle-05-01.cc new file mode 100644 index 0000000..b36c66d --- /dev/null +++ b/2021/puzzle-05-01.cc @@ -0,0 +1,68 @@ +// +// Created by Matthew Gretton-Dann on 05/12/2021. +// + +#include +#include +#include +#include +#include + +using Point = std::pair; + +auto get_points(std::string const& s) -> std::pair +{ + 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 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; +} \ No newline at end of file diff --git a/2021/puzzle-05-02.cc b/2021/puzzle-05-02.cc new file mode 100644 index 0000000..1a4848e --- /dev/null +++ b/2021/puzzle-05-02.cc @@ -0,0 +1,72 @@ +// +// Created by Matthew Gretton-Dann on 05/12/2021. +// + +#include +#include +#include +#include +#include + +using Point = std::pair; // NOLINT(google-runtime-int) + +auto get_points(std::string const& s) -> std::pair +{ + 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 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; +} \ No newline at end of file