aocpp/2022/13.cpp

197 lines
5.6 KiB
C++
Raw Normal View History

2023-03-28 16:37:47 -07:00
#include <array>
2023-01-20 10:11:49 -08:00
#include <cstdint>
#include <iostream>
#include <sstream>
#include <variant>
2023-03-28 16:49:04 -07:00
#include <vector>
#include <boost/phoenix.hpp>
#include <boost/range/adaptor/indexed.hpp>
2023-03-28 16:53:07 -07:00
#include <boost/spirit/include/qi.hpp>
2023-01-20 10:11:49 -08:00
#include <doctest.h>
#include <aocpp/Startup.hpp>
namespace {
2023-03-28 16:37:47 -07:00
namespace phx = boost::phoenix;
namespace qi = boost::spirit::qi;
2023-03-29 09:50:48 -07:00
struct Packet;
using Packets = std::vector<Packet>;
using PacketPair = std::array<Packet, 2>;
using PacketPairs = std::vector<PacketPair>;
// A packet is an integer or a list of packets.
struct Packet {
std::variant<std::int64_t, Packets> rep;
2023-01-20 10:11:49 -08:00
};
2023-03-29 09:50:48 -07:00
auto operator<(Packet const& lhs, std::int64_t rhs) -> bool;
auto operator<(std::int64_t lhs, Packet const& rhs) -> bool;
2023-03-29 09:50:48 -07:00
auto operator<(std::int64_t const lhs, Packets const& rhs) -> bool {
switch (rhs.size()) {
case 0: return false;
2023-03-29 09:50:48 -07:00
case 1: return lhs < rhs[0];
default: return !(rhs[0] < lhs);
}
}
2023-03-29 09:50:48 -07:00
auto operator<(Packets const& lhs, std::int64_t const rhs) -> bool {
switch (lhs.size()) {
case 0: return true;
2023-03-29 09:50:48 -07:00
default: return lhs[0] < rhs;
}
}
2023-03-29 09:50:48 -07:00
auto operator<(Packet const& lhs, std::int64_t rhs) -> bool {
return std::visit([rhs](auto const& rep) { return rep < rhs; }, lhs.rep);
}
2023-03-29 09:50:48 -07:00
auto operator<(std::int64_t lhs, Packet const& rhs) -> bool {
return std::visit([lhs](auto const& rep) { return lhs < rep; }, rhs.rep);
}
2023-03-29 09:50:48 -07:00
// Less-than relation on packets.
// Two integers are compared using their integer values.
// Two lists are compared using a lexicographic ordering.
// When an integer is compared to a list, that integer is promoted to a singleton list.
auto operator<(Packet const& lhs, Packet const& rhs) -> bool {
return std::visit(std::less(), lhs.rep, rhs.rep);
}
2023-03-29 09:50:48 -07:00
// Input file grammar:
// Each packet should be newline-terminated.
// Each pair should be newline-separated.
// Packets can be integer literals or a list of packets.
// A list of packets is bracketed with square brackets and is comma-separated.
// No additional whitespace is tolerated in the input stream.
2023-03-28 16:37:47 -07:00
template <typename It>
2023-03-29 09:50:48 -07:00
class Grammar : public qi::grammar<It, PacketPairs()> {
qi::rule<It, std::int64_t> integer;
qi::rule<It, Packets()> packets;
qi::rule<It, Packet()> packet;
qi::rule<It, PacketPair()> packetPair;
qi::rule<It, PacketPairs()> packetPairs;
2023-03-28 16:37:47 -07:00
public:
2023-03-29 09:50:48 -07:00
Grammar() : Grammar::base_type{packetPairs} {
2023-03-28 16:37:47 -07:00
using namespace qi::labels;
2023-03-29 09:50:48 -07:00
integer = qi::long_long;
packets = "[" >> -(packet % ",") >> "]";
packet = integer [ bind(&Packet::rep, _val) = _1 ]
| packets [ bind(&Packet::rep, _val) = _1 ];
packetPair = packet [_val[0] = _1] >> "\n" >>
packet [_val[1] = _1] >> "\n";
packetPairs = -(packetPair % "\n");
2023-01-20 10:11:49 -08:00
}
2023-03-28 16:37:47 -07:00
};
2023-01-20 10:11:49 -08:00
2023-03-29 09:50:48 -07:00
/// Parse the complete input stream according to the rules defined in 'Grammar'.
/// Throws: std::runtime_error on failed parse
auto Parse(std::istream & in) -> PacketPairs
2023-01-20 10:11:49 -08:00
{
2023-03-29 09:50:48 -07:00
auto result = PacketPairs{};
auto const content = std::string{std::istreambuf_iterator{in}, {}};
auto b = content.begin(); // updated on successful parse
auto const e = content.end();
2023-03-28 16:37:47 -07:00
if (!qi::parse(b, e, Grammar<decltype(b)>{}, result) || b != e) {
2023-03-29 09:50:48 -07:00
throw std::runtime_error{"Bad packet: " + content};
2023-01-20 10:11:49 -08:00
}
return result;
}
2023-03-29 09:50:48 -07:00
// Return the sum 1-based indexes of the pairs where the first element is less than the second.
auto Part1(PacketPairs const& packetPairs) -> std::size_t
2023-01-20 10:11:49 -08:00
{
2023-03-29 09:50:48 -07:00
auto result = std::int64_t{0};
for (auto const& [i, pair] : boost::adaptors::index(packetPairs)) {
2023-03-28 16:49:04 -07:00
if (pair[0] < pair[1]) {
2023-03-28 16:37:47 -07:00
result += i + 1;
2023-01-20 10:11:49 -08:00
}
}
return result;
}
2023-03-29 09:50:48 -07:00
// Returns the product of the 1-based indexes of the elements [[2]] and [[6]]
// when those elements are added to all the packets in the packet pair list
// and that list is then soted.
auto Part2(PacketPairs const& packetPairs) -> std::size_t
2023-01-20 10:11:49 -08:00
{
2023-03-29 09:50:48 -07:00
auto two = Packet{2}; // Indistinguishable from [[2]] and [[6]]
auto six = Packet{6};
auto ix2 = std::size_t{1};
auto ix6 = std::size_t{2};
for (auto const& pair : packetPairs) {
for (auto const& Packet : pair) {
if (Packet < six) {
2023-03-28 16:37:47 -07:00
ix6++;
2023-03-29 09:50:48 -07:00
if (Packet < two) {
2023-03-28 16:37:47 -07:00
ix2++;
}
}
}
}
return ix2 * ix6;
2023-01-20 10:11:49 -08:00
}
} // namespace
TEST_SUITE("2022-13 examples") {
TEST_CASE("simple ordering") {
2023-03-29 09:50:48 -07:00
CHECK(Packet{1} < Packet{2});
CHECK(!(Packet{2} < Packet{1}));
CHECK(Packet{Packets{Packet{1}}} < Packet{Packets{Packet{2}}});
CHECK(Packet{1} < Packet{Packets{Packet{2}}});
CHECK(!(Packet{Packets{Packet{2}}} < Packet{1}));
CHECK(Packet{Packets{}} < Packet{Packets{Packet{2}}});
CHECK(Packet{Packets{}} < Packet{Packets{Packet{2}}});
CHECK(Packet{1} < Packet{Packets{Packet{2},Packet{3}}});
CHECK(!(Packet{Packets{Packet{2},Packet{3}}} < Packet{1}));
CHECK(Packet{2} < Packet{Packets{Packet{2},Packet{3}}});
CHECK(!(Packet{Packets{Packet{2},Packet{3}}} < Packet{2}));
2023-01-20 10:11:49 -08:00
}
TEST_CASE("documented example") {
2023-03-29 09:50:48 -07:00
auto in = std::istringstream{
2023-01-20 10:11:49 -08:00
"[1,1,3,1,1]\n"
"[1,1,5,1,1]\n"
"\n"
"[[1],[2,3,4]]\n"
"[[1],4]\n"
"\n"
"[9]\n"
"[[8,7,6]]\n"
"\n"
"[[4,4],4,4]\n"
"[[4,4],4,4,4]\n"
"\n"
"[7,7,7,7]\n"
"[7,7,7]\n"
"\n"
"[]\n"
"[3]\n"
"\n"
"[[[]]]\n"
"[[]]\n"
"\n"
"[1,[2,[3,[4,[5,6,7]]]],8,9]\n"
"[1,[2,[3,[4,[5,6,0]]]],8,9]\n"
};
2023-03-29 12:02:27 -07:00
auto const packetPairs = Parse(in);
CHECK(13 == Part1(packetPairs));
CHECK(140 == Part2(packetPairs));
2023-01-20 10:11:49 -08:00
}
}
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-20 10:11:49 -08:00
{
2023-03-29 12:02:27 -07:00
auto const packetPairs = Parse(in);
out << "Part 1: " << Part1(packetPairs) << std::endl;
out << "Part 2: " << Part2(packetPairs) << std::endl;
2023-01-20 10:11:49 -08:00
}