2022-02
This commit is contained in:
parent
cded1ff25c
commit
d0c1384d7c
143
2022/02.cpp
Normal file
143
2022/02.cpp
Normal file
@ -0,0 +1,143 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
|
||||
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<std::pair<ABC, XYZ>>;
|
||||
|
||||
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;
|
||||
}
|
@ -6,6 +6,8 @@
|
||||
#include <string>
|
||||
#include <limits>
|
||||
|
||||
#include <boost/range/irange.hpp>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
@ -25,11 +27,11 @@ auto Solve(std::string const& input, std::size_t n) -> std::size_t
|
||||
uniques -= 0 == --counts[static_cast<std::uint8_t>(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; }
|
||||
|
@ -5,7 +5,6 @@
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <tuple>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Coord.hpp>
|
||||
|
@ -6,6 +6,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/range/irange.hpp>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
@ -57,7 +59,7 @@ auto Parse(std::istream & in) -> std::vector<Monkey>
|
||||
auto Solve(std::vector<Monkey> const& input, std::size_t const rounds, std::optional<std::int64_t> const modulus) -> std::int64_t
|
||||
{
|
||||
std::vector<std::int64_t> 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;) {
|
||||
|
16
2022/20.cpp
16
2022/20.cpp
@ -8,6 +8,8 @@
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/range/irange.hpp>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/DivMod.hpp>
|
||||
@ -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<std::size_t> 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<std::int64_t>(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];
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user