From 3117a35581a6cd521e132a2791da9b804cc9c90f Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Sun, 4 Dec 2022 08:12:44 +0000 Subject: [PATCH] Add 2022 Day 4. --- 2022/puzzle-04-01.cc | 44 ++++++++++++++++++++++++++++++++++++ 2022/puzzle-04-02.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 2022/puzzle-04-01.cc create mode 100644 2022/puzzle-04-02.cc diff --git a/2022/puzzle-04-01.cc b/2022/puzzle-04-01.cc new file mode 100644 index 0000000..4cddf9f --- /dev/null +++ b/2022/puzzle-04-01.cc @@ -0,0 +1,44 @@ +// +// Created by Matthew Gretton-Dann on 05/12/2021. +// + +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; +using Point = std::pair; + +auto contained_in(Point const& super, Point const& sub) noexcept -> bool +{ + return super.first <= sub.first && super.second >= sub.second; +} + +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; + UInt count{0}; + while (std::getline(std::cin, line)) { + auto [pt1, pt2] = get_points(line); + if (contained_in(pt1, pt2) || contained_in(pt2, pt1)) { + ++count; + } + } + + std::cout << "Completely duplicated effort: " << count << '\n'; + return 0; +} diff --git a/2022/puzzle-04-02.cc b/2022/puzzle-04-02.cc new file mode 100644 index 0000000..44c6506 --- /dev/null +++ b/2022/puzzle-04-02.cc @@ -0,0 +1,53 @@ +// +// Created by Matthew Gretton-Dann on 05/12/2021. +// + +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; +using Point = std::pair; + +auto contained_in(Point const& super, Point const& sub) noexcept -> bool +{ + return super.first <= sub.first && super.second >= sub.second; +} + +auto overlap(Point const& l, Point const& r) noexcept -> bool +{ + // aaaa --- bbbb + // aaCCbb + // bbCCaa + // bbbb -- aaaa + return l.second >= r.first && l.first <= r.second; +} + +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; + UInt count{0}; + while (std::getline(std::cin, line)) { + auto [pt1, pt2] = get_points(line); + if (overlap(pt2, pt1)) { + ++count; + } + } + + std::cout << "Completely duplicated effort: " << count << '\n'; + return 0; +}