From d63e62f1a79cdf7b3ce096f13e6f13c558e8e0a2 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Tue, 31 Jan 2023 21:29:04 -0800 Subject: [PATCH] 16 --- 2022/16.cpp | 38 ++++++++++++++++++-------------------- lib/src/Startup.cpp | 3 ++- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/2022/16.cpp b/2022/16.cpp index 5b84125..4cd98f4 100644 --- a/2022/16.cpp +++ b/2022/16.cpp @@ -104,7 +104,7 @@ auto Parse(std::istream & in) -> std::vector (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; -} +} \ No newline at end of file diff --git a/lib/src/Startup.cpp b/lib/src/Startup.cpp index 78aaa81..193f683 100644 --- a/lib/src/Startup.cpp +++ b/lib/src/Startup.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #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: