From d0c1384d7c443f6dd1cae62a5f46be5af29c52e8 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Wed, 25 Jan 2023 16:48:54 -0500 Subject: [PATCH] 2022-02 --- 2022/02.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++++ 2022/06.cpp | 6 +- 2022/09.cpp | 1 - 2022/11.cpp | 4 +- 2022/20.cpp | 16 ++--- 2022/CMakeLists.txt | 9 ++- CMakeLists.txt | 1 + 7 files changed, 166 insertions(+), 14 deletions(-) create mode 100644 2022/02.cpp diff --git a/2022/02.cpp b/2022/02.cpp new file mode 100644 index 0000000..75bc49e --- /dev/null +++ b/2022/02.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace { + +enum class ABC { + A, B, C +}; + +enum class XYZ { + X, Y, Z +}; + +auto xyzScore(XYZ xyz) -> std::int64_t +{ + switch (xyz) { + case XYZ::X: return 1; + case XYZ::Y: return 2; + case XYZ::Z: return 3; + } +} + +auto outcome(ABC abc, XYZ xyz) -> std::int64_t +{ + switch (abc) { + case ABC::A: + switch (xyz) { + case XYZ::X: return 3; + case XYZ::Y: return 6; + case XYZ::Z: return 0; + } + case ABC::B: + switch (xyz) { + case XYZ::X: return 0; + case XYZ::Y: return 3; + case XYZ::Z: return 6; + } + case ABC::C: + switch (xyz) { + case XYZ::X: return 6; + case XYZ::Y: return 0; + case XYZ::Z: return 3; + } + } +} + +auto DesiredOutcome(XYZ xyz) -> std::int64_t +{ + switch (xyz) { + case XYZ::X: return 0; + case XYZ::Y: return 3; + case XYZ::Z: return 6; + } +} + +auto Pick(ABC abc, XYZ xyz) -> XYZ +{ + for (auto result : {XYZ::X, XYZ::Y, XYZ::Z}) { + if (outcome(abc, result) == DesiredOutcome(xyz)) { + return result; + } + } + throw std::runtime_error{"Pick couldn't find result"}; +} + +using Input = std::vector>; + +auto Parse(std::istream & in) -> Input +{ + Input result; + std::string abc, xyz; + + while (in >> abc >> xyz) { + result.emplace_back( + abc == "A" ? ABC::A : + abc == "B" ? ABC::B : + ABC::C, + xyz == "X" ? XYZ::X : + xyz == "Y" ? XYZ::Y : + XYZ::Z + ); + } + + return result; +} + +auto Part1(Input const& input) -> std::int64_t +{ + return std::transform_reduce( + input.begin(), input.end(), + std::int64_t{0}, + std::plus(), + [](auto x) { + auto [abc, xyz] = x; + return outcome(abc, xyz) + xyzScore(xyz); + }); +} + +auto Part2(Input const& input) -> std::int64_t +{ + return std::transform_reduce( + input.begin(), input.end(), + std::int64_t{0}, + std::plus(), + [](auto x) { + auto [abc, xyz] = x; + xyz = Pick(abc, xyz); + return outcome(abc, xyz) + xyzScore(xyz); + }); +} + +} // namespace + +TEST_SUITE("2022-02 examples") { + TEST_CASE("example") { + std::istringstream in { +R"(A Y +B X +C Z +)" + }; + auto const input = Parse(in); + CHECK(Part1(input) == 15); + CHECK(Part2(input) == 12); + } +} + +auto main(int argc, char** argv) -> int { + auto const input {Parse(*aocpp::Startup(argc, argv))} ; + std::cout << "Part 1: " << Part1(input) << std::endl; + std::cout << "Part 2: " << Part2(input) << std::endl; +} diff --git a/2022/06.cpp b/2022/06.cpp index 19b69fb..1bde1cb 100644 --- a/2022/06.cpp +++ b/2022/06.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include #include @@ -25,11 +27,11 @@ auto Solve(std::string const& input, std::size_t n) -> std::size_t uniques -= 0 == --counts[static_cast(c)]; }; - for (std::size_t i = 0; i < n; ++i) { + for (auto const i : boost::irange(n)) { inc(input[i]); } - for (std::size_t i = n; i < input.size(); ++i) { + for (auto const i : boost::irange(n, input.size())) { dec(input[i-n]); inc(input[i]); if (uniques == n) { return i+1; } diff --git a/2022/09.cpp b/2022/09.cpp index ef438c9..c044bb5 100644 --- a/2022/09.cpp +++ b/2022/09.cpp @@ -5,7 +5,6 @@ #include #include #include - #include #include diff --git a/2022/11.cpp b/2022/11.cpp index 5d30bee..93bb11e 100644 --- a/2022/11.cpp +++ b/2022/11.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include #include @@ -57,7 +59,7 @@ auto Parse(std::istream & in) -> std::vector auto Solve(std::vector const& input, std::size_t const rounds, std::optional const modulus) -> std::int64_t { std::vector throws(input.size()); - for (std::size_t i = 0; i < input.size(); ++i) { + for (auto const i : boost::irange(input.size())) { for (auto item : input[i].items) { auto cursor = i; for (auto n = rounds; n > 0;) { diff --git a/2022/20.cpp b/2022/20.cpp index 879d1e7..8c0f635 100644 --- a/2022/20.cpp +++ b/2022/20.cpp @@ -8,6 +8,8 @@ #include #include +#include + #include #include @@ -30,14 +32,14 @@ auto Parse(std::istream & in) -> Input auto Solve(Input const& input, std::size_t const rounds) -> std::int64_t { - std::size_t const len = input.size(); + auto const len = input.size(); std::vector fwds(len), bwds(len); std::iota(fwds.begin(), fwds.end()-1, 1); fwds.back() = 0; std::iota(bwds.begin()+1, bwds.end(), 0); bwds.front() = len-1; - for (std::size_t r = 0; r < rounds; ++r) { - for (std::size_t i = 0; i < len; ++i) { + for (auto const _ : boost::irange(rounds)) { + for (auto const i : boost::irange(len)) { // Remove i from the ring auto next = fwds[i]; @@ -47,11 +49,11 @@ auto Solve(Input const& input, std::size_t const rounds) -> std::int64_t auto const steps = aocpp::Mod(input[i], len - 1); if (steps < len/2) { - for (std::size_t s = 0; s < steps; ++s) { + for (auto const _ : boost::irange(steps)) { next = fwds[next]; } } else { - for (std::size_t s = 0; s < len - 1 - steps; ++s) { + for (auto const _ : boost::irange(len - 1 - steps)) { next = bwds[next]; } } @@ -67,8 +69,8 @@ auto Solve(Input const& input, std::size_t const rounds) -> std::int64_t std::int64_t sum {0}; auto cursor = std::distance(input.begin(), std::find(input.begin(), input.end(), 0)); - for (std::size_t r = 0; r < 3; ++r) { - for (std::size_t s = 0; s < 1000; ++s) { + for (auto const _ : boost::irange(3)) { + for (auto const _ : boost::irange(1000)) { cursor = fwds[cursor]; } sum += input[cursor]; diff --git a/2022/CMakeLists.txt b/2022/CMakeLists.txt index db33860..b4aa3b1 100644 --- a/2022/CMakeLists.txt +++ b/2022/CMakeLists.txt @@ -1,6 +1,9 @@ add_executable(2022_01 01.cpp) target_link_libraries(2022_01 aocpp) +add_executable(2022_02 02.cpp) +target_link_libraries(2022_02 aocpp) + add_executable(2022_03 03.cpp) target_link_libraries(2022_03 aocpp) @@ -8,7 +11,7 @@ add_executable(2022_04 04.cpp) target_link_libraries(2022_04 aocpp) add_executable(2022_06 06.cpp) -target_link_libraries(2022_06 aocpp) +target_link_libraries(2022_06 aocpp Boost::headers) add_executable(2022_08 08.cpp) target_link_libraries(2022_08 aocpp) @@ -20,7 +23,7 @@ add_executable(2022_10 10.cpp) target_link_libraries(2022_10 aocpp) add_executable(2022_11 11.cpp) -target_link_libraries(2022_11 aocpp) +target_link_libraries(2022_11 aocpp Boost::headers) add_executable(2022_12 12.cpp) target_link_libraries(2022_12 aocpp) @@ -32,7 +35,7 @@ add_executable(2022_18 18.cpp) target_link_libraries(2022_18 aocpp) add_executable(2022_20 20.cpp) -target_link_libraries(2022_20 aocpp) +target_link_libraries(2022_20 aocpp Boost::headers) add_executable(2022_25 25.cpp) target_link_libraries(2022_25 aocpp) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e45604..32a8bd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ endif() find_package(PkgConfig) pkg_check_modules(GMP REQUIRED IMPORTED_TARGET gmpxx) +find_package(Boost REQUIRED) add_subdirectory(lib) add_subdirectory(dlx)