Use a common main

This commit is contained in:
Eric Mertens
2023-01-31 08:58:42 -08:00
parent 542ccc7884
commit c6264a2a1b
63 changed files with 222 additions and 174 deletions

View File

@@ -44,10 +44,8 @@ TEST_CASE("part 2") {
}
auto main(int argc, char** argv) -> int {
auto in_ptr = aocpp::Startup(argc, argv);
auto & in = *in_ptr;
auto Main(std::istream & in) -> void
{
std::int64_t weight;
std::int64_t part1 = 0;
std::int64_t part2 = 0;

View File

@@ -36,8 +36,9 @@ TEST_SUITE("documented examples") {
}
}
auto main(int argc, char** argv) -> int {
auto machine = Machine{ParseStream(*aocpp::Startup(argc, argv))};
auto Main(std::istream & in) -> void
{
auto machine = Machine{ParseStream(in)};
std::cout << "Part 1: " << Compute(machine, 12, 2) << std::endl;

View File

@@ -66,10 +66,8 @@ auto Record (
} // namespace
auto main(int argc, char** argv) -> int {
auto in_ptr = aocpp::Startup(argc, argv);
auto & in = *in_ptr;
auto Main(std::istream & in) -> void
{
std::string line;
std::getline(in, line);
auto wire0 = BuildLine(line);

View File

@@ -43,8 +43,9 @@ auto Valid2(std::string const& str) {
} // namespace
auto main(int argc, char** argv) -> int {
auto [lo,hi] = Parse(*aocpp::Startup(argc, argv));
auto Main(std::istream & in) -> void
{
auto const [lo,hi] = Parse(in);
std::int64_t part1 = 0;
std::int64_t part2 = 0;

View File

@@ -17,8 +17,9 @@ auto Compute(Machine machine, ValueType d) -> ValueType {
} // namespace
auto main(int argc, char** argv) -> int {
auto machine = Machine{ParseStream(*aocpp::Startup(argc, argv))};
auto Main(std::istream & in) -> void
{
auto machine = Machine{ParseStream(in)};
std::cout << "Part 1: " << Compute(machine, 1) << std::endl;
std::cout << "Part 2: " << Compute(std::move(machine), 5) << std::endl;
}

View File

@@ -88,8 +88,9 @@ TEST_SUITE("documented examples") {
}
}
auto main(int argc, char** argv) -> int {
auto parents = Parse(*aocpp::Startup(argc, argv));
auto Main(std::istream & in) -> void
{
auto const parents {Parse(in)};
std::cout << "Part 1: " << Part1(parents) << std::endl;
std::cout << "Part 2: " << Part2(parents) << std::endl;
}

View File

@@ -85,8 +85,9 @@ TEST_SUITE("documented examples") {
}
}
auto main(int argc, char** argv) -> int {
auto machine = Machine{ParseStream(*aocpp::Startup(argc, argv))};
auto Main(std::istream & in) -> void
{
auto machine = Machine{ParseStream(in)};
std::cout << "Part 1: " << Part1(machine) << std::endl;
std::cout << "Part 2: " << Part2(std::move(machine)) << std::endl;
}

View File

@@ -56,9 +56,10 @@ auto Draw(std::string const& picture) {
} // namespace
auto main(int argc, char** argv) -> int {
auto Main(std::istream & in) -> void
{
std::string line;
std::getline(*aocpp::Startup(argc, argv), line);
std::getline(in, line);
std::cout << "Part 1: " << Part1(line) << std::endl;
Draw(Flatten(line));
}

View File

@@ -29,8 +29,9 @@ TEST_SUITE("documented examples") {
}
}
auto main(int argc, char** argv) -> int {
auto machine = Machine{ParseStream(*aocpp::Startup(argc, argv))};
auto Main(std::istream & in) -> void
{
auto machine = Machine{ParseStream(in)};
std::cout << "Part 1: " << Compute(machine, 1) << std::endl;
std::cout << "Part 2: " << Compute(std::move(machine), 2) << std::endl;
}

View File

@@ -208,9 +208,10 @@ TEST_SUITE("documented examples") {
}
}
auto main(int argc, char** argv) -> int {
auto grid = Grid::Parse(*Startup(argc, argv));
auto [part1, base] = Part1(grid);
auto Main(std::istream & in) -> void
{
auto const grid {Grid::Parse(in)};
auto const [part1, base] = Part1(grid);
std::cout << "Part 1: " << part1 << std::endl;
std::cout << "Part 2: " << Part2(grid, base, 200) << std::endl;
}

View File

@@ -37,8 +37,9 @@ auto Compute(Machine machine, ValueType start)
} // namespace
auto main(int argc, char** argv) -> int {
auto machine = Machine{ParseStream(*aocpp::Startup(argc, argv))};
auto Main(std::istream & in) -> void
{
auto machine = Machine{ParseStream(in)};
std::cout << "Part 1: " << Compute(machine, 0).size() << "\nPart 2\n";
Draw(std::cout, Compute(std::move(machine), 1));
}

View File

@@ -93,10 +93,11 @@ auto Part2(std::array<std::vector<Particle>, 3> const& ps) {
} // namespace
auto main(int argc, char** argv) -> int {
auto ps = Parse(*aocpp::Startup(argc, argv));
auto part2 = Part2(ps);
auto part1 = Part1(std::move(ps), 1000);
auto Main(std::istream & in) -> void
{
auto ps = Parse(in);
auto const part2 = Part2(ps);
auto const part1 = Part1(std::move(ps), 1000);
std::cout << "Part 1: " << part1 << std::endl;
std::cout << "Part 2: " << part2 << std::endl;

View File

@@ -59,8 +59,9 @@ auto Compute2(Machine machine) {
} // namespace
auto main(int argc, char** argv) -> int {
auto machine = Machine{ParseStream(*aocpp::Startup(argc, argv))};
auto Main(std::istream & in) -> void
{
auto machine = Machine{ParseStream(in)};
std::cout << "Part 1: " << Compute1(machine) << std::endl;
std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl;
}

View File

@@ -228,11 +228,12 @@ TEST_SUITE("documented examples") {
}
}
auto main(int argc, char** argv) -> int {
auto recipes = Parse(*aocpp::Startup(argc, argv));
auto machine = Machine(recipes);
auto part1 = machine(1);
auto part2 = ComputeFuel(machine, 1'000'000'000'000);
auto Main(std::istream & in) -> void
{
auto const recipes {Parse(in)};
auto machine {Machine(recipes)};
auto const part1 {machine(1)};
auto const part2 {ComputeFuel(machine, 1'000'000'000'000)};
std::cout << "Part 1: " << part1 << std::endl;
std::cout << "Part 2: " << part2 << std::endl;
}

View File

@@ -92,8 +92,9 @@ auto Compute(std::map<Coord, ValueType> world) -> std::pair<int, int> {
} // namespace
auto main(int argc, char** argv) -> int {
auto [p1,p2] = Compute(Explore(Machine{ParseStream(*aocpp::Startup(argc, argv))}));
auto Main(std::istream & in) -> void
{
auto const [p1,p2] = Compute(Explore(Machine{ParseStream(in)}));
std::cout << "Part 1: " << p1 << std::endl;
std::cout << "Part 2: " << p2 << std::endl;
}

View File

@@ -118,8 +118,9 @@ TEST_SUITE("documented examples") {
}
}
auto main(int argc, char** argv) -> int {
auto input = Parse(*aocpp::Startup(argc, argv));
auto Main(std::istream & in) -> void
{
auto const input = Parse(in);
std::cout << "Part 1: " << Compute(input, 100, 0) << std::endl;
std::cout << "Part 2: " << Part2(input) << std::endl;

View File

@@ -213,14 +213,15 @@ auto GatherScore(Machine m, std::string const& program) -> ValueType {
} // namespace
auto main(int argc, char** argv) -> int {
auto machine = Machine{ParseStream(*aocpp::Startup(argc, argv))};
auto grid = GatherOutput(machine);
auto part1 = ScaffoldAlignments(grid);
auto path = ComputePath(grid);
auto program = ComputeProgram(path);
auto Main(std::istream & in) -> void
{
auto machine = Machine{ParseStream(in)};
auto const grid = GatherOutput(machine);
auto const part1 = ScaffoldAlignments(grid);
auto const path = ComputePath(grid);
auto const program = ComputeProgram(path);
std::cout << "Part 1: " << part1 << std::endl;
auto part2 = GatherScore(std::move(machine), program);
auto const part2 = GatherScore(std::move(machine), program);
std::cout << "Part 2: " << part2 << std::endl;
}

View File

@@ -160,8 +160,9 @@ auto Part2(Grid & grid, Features & features) {
} // namespace
auto main(int argc, char** argv) -> int {
auto grid = Grid::Parse(*Startup(argc, argv));
auto Main(std::istream & in) -> void
{
auto grid = Grid::Parse(in);
auto features = FindFeatures(grid);
auto distances = FindDistances(grid, features);
std::cout << "Part 1: " << SolveMaze(distances, "@") << std::endl;

View File

@@ -45,8 +45,9 @@ auto Part2(Scanner const& scanner) {
} // namespace
auto main(int argc, char** argv) -> int {
auto scanner = Scanner{Machine{ParseStream(*aocpp::Startup(argc, argv))}};
auto Main(std::istream & in) -> void
{
auto const scanner = Scanner{Machine{ParseStream(in)}};
std::cout << "Part 1: " << Part1(scanner) << std::endl;
std::cout << "Part 2: " << Part2(scanner) << std::endl;
}

View File

@@ -275,10 +275,11 @@ TEST_SUITE("2019-20 examples") {
}
auto main(int argc, char** argv) -> int {
auto map = Grid::Parse(*Startup(argc, argv));
auto portals = FindPortals(map);
auto distances = FindDistances(map, portals);
auto Main(std::istream & in) -> void
{
auto const map {Grid::Parse(in)};
auto const portals {FindPortals(map)};
auto const distances {FindDistances(map, portals)};
std::cout << "Part 1: " << SolveMaze(distances, false) << std::endl;
std::cout << "Part 2: " << SolveMaze(distances, true) << std::endl;

View File

@@ -262,8 +262,9 @@ auto Compute(
} // namespace
auto main(int argc, char** argv) -> int {
Machine machine {ParseStream(*aocpp::Startup(argc, argv))};
auto Main(std::istream & in) -> void
{
Machine machine {ParseStream(in)};
Compute(machine, 4,
{ "#####.###########",

View File

@@ -114,13 +114,15 @@ auto Many(Shuffle<Cards> shuffle, unsigned long n) -> Shuffle<Cards> {
} // namespace
auto main(int argc, char** argv) -> int {
auto instructions = Parse(*Startup(argc, argv));
auto Main(std::istream & in) -> void
{
auto const instructions {Parse(in)};
auto shuffle1 = FollowInstructions<10007>(instructions);
auto const shuffle1 {FollowInstructions<10007>(instructions)};
std::cout << "Part 1: " << shuffle1(2019) << std::endl;
auto shuffle2 = FollowInstructions<119315717514047>(instructions);
auto shuffle2 {FollowInstructions<119315717514047>(instructions)};
shuffle2 = Many(shuffle2, 101741582076661);
std::cout << "Part 2: " << shuffle2.inverse()(2020) << std::endl;
shuffle2 = shuffle2.inverse();
std::cout << "Part 2: " << shuffle2(2020) << std::endl;
}

View File

@@ -55,8 +55,9 @@ auto Interact(Ethernet & ethernet, Machine & m, std::optional<Payload> p) -> voi
} // namespace
auto main(int const argc, char** const argv) -> int {
auto machines = BuildNetwork(Machine{ParseStream(*aocpp::Startup(argc, argv))});
auto Main(std::istream & in) -> void
{
auto machines = BuildNetwork(Machine{ParseStream(in)});
auto ethernet = Ethernet{};
std::optional<ValueType> part1;

View File

@@ -135,8 +135,9 @@ TEST_SUITE("documented examples") {
}
}
auto main(int argc, char** argv) -> int {
auto const bugs = FindBugs(Grid::Parse(*Startup(argc, argv)));
auto Main(std::istream & in) -> void
{
auto const bugs {FindBugs(Grid::Parse(in))};
std::cout << "Part 1: " << Part1(bugs) << std::endl;
std::cout << "Part 2: " << Part2(std::move(bugs), 200) << std::endl;
}

View File

@@ -58,7 +58,8 @@ auto Search(std::string & script)
} // namespace
auto main(int argc, char** argv) -> int {
auto Main(std::istream & in) -> void
{
std::string script =
"north\n" "take sand\n"
"north\n" "take space heater\n"
@@ -76,5 +77,5 @@ auto main(int argc, char** argv) -> int {
Search(script);
RunWithIO(Machine{ParseStream(*aocpp::Startup(argc, argv))}, script);
RunWithIO(Machine{ParseStream(in)}, script);
}