This commit is contained in:
Eric Mertens 2023-01-31 21:29:04 -08:00
parent 8004218a79
commit d63e62f1a7
2 changed files with 20 additions and 21 deletions

View File

@ -104,7 +104,7 @@ auto Parse(std::istream & in) -> std::vector<Room>
(name % ", ") [ phx::bind(&Room::connections, _val) = _1 ]; (name % ", ") [ phx::bind(&Room::connections, _val) = _1 ];
It b = line.begin(); It b = line.begin();
It e = line.end(); It const e = line.end();
result.emplace_back(); result.emplace_back();
if (!qi::parse(b, e, room_description, result.back()) || b != e) { if (!qi::parse(b, e, room_description, result.back()) || b != e) {
throw std::runtime_error{"bad input line"}; throw std::runtime_error{"bad input line"};
@ -273,6 +273,19 @@ auto Part2(
} // namespace } // namespace
/// @brief Print solutions to parts 1 and 2
/// @param[in,out] in input text
/// @param[in,out] out output text
auto Main(std::istream & in, std::ostream & out) -> void
{
auto rooms = Parse(in);
auto const n = FlowsFirst(rooms);
auto const [start, distances] = GenerateDistances(rooms);
rooms.resize(n); // forget about the rooms with no flow
out << "Part 1: " << Part1(start, rooms, distances) << std::endl;
out << "Part 2: " << Part2(start, rooms, distances) << std::endl;
}
TEST_SUITE("2022-16") { TEST_SUITE("2022-16") {
TEST_CASE("example") { TEST_CASE("example") {
std::istringstream in { std::istringstream in {
@ -287,12 +300,9 @@ Valve HH has flow rate=22; tunnel leads to valve GG
Valve II has flow rate=0; tunnels lead to valves AA, JJ Valve II has flow rate=0; tunnels lead to valves AA, JJ
Valve JJ has flow rate=21; tunnel leads to valve II Valve JJ has flow rate=21; tunnel leads to valve II
)"}; )"};
auto rooms = Parse(in); std::ostringstream out;
auto const n = FlowsFirst(rooms); Main(in, out);
auto const [start, distances] = GenerateDistances(rooms); CHECK(out.str() == "Part 1: 1651\nPart 2: 1707\n");
rooms.resize(n);
CHECK(1651 == Part1(start, rooms, distances));
CHECK(1707 == Part2(start, rooms, distances));
} }
TEST_CASE("shortest path") { TEST_CASE("shortest path") {
@ -331,15 +341,3 @@ Valve JJ has flow rate=21; tunnel leads to valve II
CHECK(distances[3][3] == 0); CHECK(distances[3][3] == 0);
} }
} }
/// @brief Print solutions to parts 1 and 2
/// @param in selected input stream
auto Main(std::istream & in, std::ostream & out) -> void
{
auto rooms = Parse(in);
auto const n = FlowsFirst(rooms);
auto const [start, distances] = GenerateDistances(rooms);
rooms.resize(n);
out << "Part 1: " << Part1(start, rooms, distances) << std::endl;
out << "Part 2: " << Part2(start, rooms, distances) << std::endl;
}

View File

@ -5,6 +5,7 @@
#include <fstream> #include <fstream>
#include <utility> #include <utility>
#include <libgen.h>
#include <unistd.h> #include <unistd.h>
#define DOCTEST_CONFIG_IMPLEMENT #define DOCTEST_CONFIG_IMPLEMENT
@ -27,7 +28,7 @@ auto main(int argc, char ** argv) -> int
std::cerr << "Missing argument to -" << char(optopt) << std::endl; std::cerr << "Missing argument to -" << char(optopt) << std::endl;
return 1; return 1;
case 'h': case 'h':
std::cerr << "Usage: " << argv[0] << " [-i INPUT] [-o OUTPUT] [-t]" << std::endl; std::cerr << "Usage: " << basename(argv[0]) << " [-i INPUT] [-o OUTPUT] [-t]" << std::endl;
return 0; return 0;
case '?': case '?':
default: default: