16
This commit is contained in:
parent
8004218a79
commit
d63e62f1a7
38
2022/16.cpp
38
2022/16.cpp
@ -104,7 +104,7 @@ auto Parse(std::istream & in) -> std::vector<Room>
|
||||
(name % ", ") [ phx::bind(&Room::connections, _val) = _1 ];
|
||||
|
||||
It b = line.begin();
|
||||
It e = line.end();
|
||||
It const e = line.end();
|
||||
result.emplace_back();
|
||||
if (!qi::parse(b, e, room_description, result.back()) || b != e) {
|
||||
throw std::runtime_error{"bad input line"};
|
||||
@ -273,6 +273,19 @@ auto Part2(
|
||||
|
||||
} // 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_CASE("example") {
|
||||
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 JJ has flow rate=21; tunnel leads to valve II
|
||||
)"};
|
||||
auto rooms = Parse(in);
|
||||
auto const n = FlowsFirst(rooms);
|
||||
auto const [start, distances] = GenerateDistances(rooms);
|
||||
rooms.resize(n);
|
||||
CHECK(1651 == Part1(start, rooms, distances));
|
||||
CHECK(1707 == Part2(start, rooms, distances));
|
||||
std::ostringstream out;
|
||||
Main(in, out);
|
||||
CHECK(out.str() == "Part 1: 1651\nPart 2: 1707\n");
|
||||
}
|
||||
|
||||
TEST_CASE("shortest path") {
|
||||
@ -330,16 +340,4 @@ Valve JJ has flow rate=21; tunnel leads to valve II
|
||||
CHECK(distances[3][2] == 1);
|
||||
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;
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
#include <fstream>
|
||||
#include <utility>
|
||||
|
||||
#include <libgen.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define DOCTEST_CONFIG_IMPLEMENT
|
||||
@ -27,7 +28,7 @@ auto main(int argc, char ** argv) -> int
|
||||
std::cerr << "Missing argument to -" << char(optopt) << std::endl;
|
||||
return 1;
|
||||
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;
|
||||
case '?':
|
||||
default:
|
||||
|
Loading…
Reference in New Issue
Block a user