make output stream a Main parameter
This commit is contained in:
@@ -44,7 +44,7 @@ TEST_CASE("part 2") {
|
||||
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
std::int64_t weight;
|
||||
std::int64_t part1 = 0;
|
||||
@@ -55,6 +55,6 @@ auto Main(std::istream & in) -> void
|
||||
part2 += fuel2(weight);
|
||||
}
|
||||
|
||||
std::cout << "Part 1: " << part1 << std::endl;
|
||||
std::cout << "Part 2: " << part2 << std::endl;
|
||||
out << "Part 1: " << part1 << std::endl;
|
||||
out << "Part 2: " << part2 << std::endl;
|
||||
}
|
||||
|
@@ -36,16 +36,16 @@ TEST_SUITE("documented examples") {
|
||||
}
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto machine = Machine{ParseStream(in)};
|
||||
|
||||
std::cout << "Part 1: " << Compute(machine, 12, 2) << std::endl;
|
||||
out << "Part 1: " << Compute(machine, 12, 2) << std::endl;
|
||||
|
||||
for (std::int64_t i = 0; i < 100; i++) {
|
||||
for (std::int64_t j = 0; j < 100; j++) {
|
||||
if (19690720 == Compute(machine, i, j)) {
|
||||
std::cout << "Part 2: " << 100 * i + j << std::endl;
|
||||
out << "Part 2: " << 100 * i + j << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -66,7 +66,7 @@ auto Record (
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
std::string line;
|
||||
std::getline(in, line);
|
||||
@@ -88,6 +88,6 @@ auto Main(std::istream & in) -> void
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Part 1: " << part1 << std::endl;
|
||||
std::cout << "Part 2: " << part2 << std::endl;
|
||||
out << "Part 1: " << part1 << std::endl;
|
||||
out << "Part 2: " << part2 << std::endl;
|
||||
}
|
||||
|
@@ -43,7 +43,7 @@ auto Valid2(std::string const& str) {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto const [lo,hi] = Parse(in);
|
||||
|
||||
@@ -58,6 +58,6 @@ auto Main(std::istream & in) -> void
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Part 1: " << part1 << std::endl;
|
||||
std::cout << "Part 2: " << part2 << std::endl;
|
||||
out << "Part 1: " << part1 << std::endl;
|
||||
out << "Part 2: " << part2 << std::endl;
|
||||
}
|
||||
|
@@ -17,9 +17,9 @@ auto Compute(Machine machine, ValueType d) -> ValueType {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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;
|
||||
out << "Part 1: " << Compute(machine, 1) << std::endl;
|
||||
out << "Part 2: " << Compute(std::move(machine), 5) << std::endl;
|
||||
}
|
||||
|
@@ -88,9 +88,9 @@ TEST_SUITE("documented examples") {
|
||||
}
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto const parents {Parse(in)};
|
||||
std::cout << "Part 1: " << Part1(parents) << std::endl;
|
||||
std::cout << "Part 2: " << Part2(parents) << std::endl;
|
||||
out << "Part 1: " << Part1(parents) << std::endl;
|
||||
out << "Part 2: " << Part2(parents) << std::endl;
|
||||
}
|
||||
|
@@ -85,9 +85,9 @@ TEST_SUITE("documented examples") {
|
||||
}
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto machine = Machine{ParseStream(in)};
|
||||
std::cout << "Part 1: " << Part1(machine) << std::endl;
|
||||
std::cout << "Part 2: " << Part2(std::move(machine)) << std::endl;
|
||||
out << "Part 1: " << Part1(machine) << std::endl;
|
||||
out << "Part 2: " << Part2(std::move(machine)) << std::endl;
|
||||
}
|
||||
|
12
2019/08.cpp
12
2019/08.cpp
@@ -45,21 +45,21 @@ auto Flatten(std::string const& line) {
|
||||
return merged;
|
||||
}
|
||||
|
||||
auto Draw(std::string const& picture) {
|
||||
auto Draw(std::ostream & out, std::string const& picture) {
|
||||
for (std::size_t y = 0; y < 6; y++) {
|
||||
for (std::size_t x = 0; x < 25; x++) {
|
||||
std::cout << ('1' == picture[y*25+x] ? "▓" : "░");
|
||||
out << ('1' == picture[y*25+x] ? "▓" : "░");
|
||||
}
|
||||
std::cout << std::endl;
|
||||
out << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
std::string line;
|
||||
std::getline(in, line);
|
||||
std::cout << "Part 1: " << Part1(line) << std::endl;
|
||||
Draw(Flatten(line));
|
||||
out << "Part 1: " << Part1(line) << std::endl;
|
||||
Draw(out, Flatten(line));
|
||||
}
|
||||
|
@@ -29,9 +29,9 @@ TEST_SUITE("documented examples") {
|
||||
}
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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;
|
||||
out << "Part 1: " << Compute(machine, 1) << std::endl;
|
||||
out << "Part 2: " << Compute(std::move(machine), 2) << std::endl;
|
||||
}
|
||||
|
@@ -208,10 +208,10 @@ TEST_SUITE("documented examples") {
|
||||
}
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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;
|
||||
out << "Part 1: " << part1 << std::endl;
|
||||
out << "Part 2: " << Part2(grid, base, 200) << std::endl;
|
||||
}
|
||||
|
@@ -37,9 +37,9 @@ auto Compute(Machine machine, ValueType start)
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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));
|
||||
out << "Part 1: " << Compute(machine, 0).size() << "\nPart 2\n";
|
||||
Draw(out, Compute(std::move(machine), 1));
|
||||
}
|
||||
|
@@ -93,12 +93,12 @@ auto Part2(std::array<std::vector<Particle>, 3> const& ps) {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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;
|
||||
out << "Part 1: " << part1 << std::endl;
|
||||
out << "Part 2: " << part2 << std::endl;
|
||||
}
|
||||
|
@@ -59,9 +59,9 @@ auto Compute2(Machine machine) {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto machine = Machine{ParseStream(in)};
|
||||
std::cout << "Part 1: " << Compute1(machine) << std::endl;
|
||||
std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl;
|
||||
out << "Part 1: " << Compute1(machine) << std::endl;
|
||||
out << "Part 2: " << Compute2(std::move(machine)) << std::endl;
|
||||
}
|
||||
|
@@ -228,12 +228,12 @@ TEST_SUITE("documented examples") {
|
||||
}
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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;
|
||||
out << "Part 1: " << part1 << std::endl;
|
||||
out << "Part 2: " << part2 << std::endl;
|
||||
}
|
||||
|
@@ -92,9 +92,9 @@ auto Compute(std::map<Coord, ValueType> world) -> std::pair<int, int> {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto const [p1,p2] = Compute(Explore(Machine{ParseStream(in)}));
|
||||
std::cout << "Part 1: " << p1 << std::endl;
|
||||
std::cout << "Part 2: " << p2 << std::endl;
|
||||
out << "Part 1: " << p1 << std::endl;
|
||||
out << "Part 2: " << p2 << std::endl;
|
||||
}
|
||||
|
@@ -118,10 +118,10 @@ TEST_SUITE("documented examples") {
|
||||
}
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto const input = Parse(in);
|
||||
|
||||
std::cout << "Part 1: " << Compute(input, 100, 0) << std::endl;
|
||||
std::cout << "Part 2: " << Part2(input) << std::endl;
|
||||
out << "Part 1: " << Compute(input, 100, 0) << std::endl;
|
||||
out << "Part 2: " << Part2(input) << std::endl;
|
||||
}
|
||||
|
@@ -213,15 +213,15 @@ auto GatherScore(Machine m, std::string const& program) -> ValueType {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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;
|
||||
out << "Part 1: " << part1 << std::endl;
|
||||
|
||||
auto const part2 = GatherScore(std::move(machine), program);
|
||||
std::cout << "Part 2: " << part2 << std::endl;
|
||||
out << "Part 2: " << part2 << std::endl;
|
||||
}
|
||||
|
@@ -160,14 +160,14 @@ auto Part2(Grid & grid, Features & features) {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto grid = Grid::Parse(in);
|
||||
auto features = FindFeatures(grid);
|
||||
auto distances = FindDistances(grid, features);
|
||||
std::cout << "Part 1: " << SolveMaze(distances, "@") << std::endl;
|
||||
out << "Part 1: " << SolveMaze(distances, "@") << std::endl;
|
||||
|
||||
Part2(grid, features);
|
||||
distances = FindDistances(grid, features);
|
||||
std::cout << "Part 2: " << SolveMaze(distances, "^&*$") << std::endl;
|
||||
out << "Part 2: " << SolveMaze(distances, "^&*$") << std::endl;
|
||||
}
|
||||
|
@@ -45,9 +45,9 @@ auto Part2(Scanner const& scanner) {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto const scanner = Scanner{Machine{ParseStream(in)}};
|
||||
std::cout << "Part 1: " << Part1(scanner) << std::endl;
|
||||
std::cout << "Part 2: " << Part2(scanner) << std::endl;
|
||||
out << "Part 1: " << Part1(scanner) << std::endl;
|
||||
out << "Part 2: " << Part2(scanner) << std::endl;
|
||||
}
|
||||
|
@@ -275,12 +275,12 @@ TEST_SUITE("2019-20 examples") {
|
||||
}
|
||||
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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;
|
||||
out << "Part 1: " << SolveMaze(distances, false) << std::endl;
|
||||
out << "Part 2: " << SolveMaze(distances, true) << std::endl;
|
||||
}
|
||||
|
14
2019/21.cpp
14
2019/21.cpp
@@ -229,9 +229,9 @@ auto Compute(
|
||||
auto const output = RunStream(machine, in, out);
|
||||
|
||||
if (output > 0) {
|
||||
std::cout << "Hull damage: " << output << std::endl;
|
||||
out << "Hull damage: " << output << std::endl;
|
||||
} else {
|
||||
std::cout << "Learned " << GetCounterExample(out) << std::endl;
|
||||
out << "Learned " << GetCounterExample(out) << std::endl;
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::size_t>> sensors {{}};
|
||||
@@ -243,13 +243,13 @@ auto Compute(
|
||||
searching = false;
|
||||
for (bool const p : {true,false}) {
|
||||
auto const terms = QuineMcCluskey(p, sensor.size(), cases);
|
||||
std::cout << (p ? "\nTRUE\n" : "\nFALSE\n");
|
||||
out << (p ? "\nTRUE\n" : "\nFALSE\n");
|
||||
for (auto const s : sensor) {
|
||||
std::cout << char('A'+s-1);
|
||||
out << char('A'+s-1);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
out << std::endl;
|
||||
for (auto const& term : terms) {
|
||||
std::cout << term << std::endl;
|
||||
out << term << std::endl;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -262,7 +262,7 @@ auto Compute(
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
Machine machine {ParseStream(in)};
|
||||
|
||||
|
@@ -114,15 +114,15 @@ auto Many(Shuffle<Cards> shuffle, unsigned long n) -> Shuffle<Cards> {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto const instructions {Parse(in)};
|
||||
|
||||
auto const shuffle1 {FollowInstructions<10007>(instructions)};
|
||||
std::cout << "Part 1: " << shuffle1(2019) << std::endl;
|
||||
out << "Part 1: " << shuffle1(2019) << std::endl;
|
||||
|
||||
auto shuffle2 {FollowInstructions<119315717514047>(instructions)};
|
||||
shuffle2 = Many(shuffle2, 101741582076661);
|
||||
shuffle2 = shuffle2.inverse();
|
||||
std::cout << "Part 2: " << shuffle2(2020) << std::endl;
|
||||
out << "Part 2: " << shuffle2(2020) << std::endl;
|
||||
}
|
||||
|
@@ -55,7 +55,7 @@ auto Interact(Ethernet & ethernet, Machine & m, std::optional<Payload> p) -> voi
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
auto machines = BuildNetwork(Machine{ParseStream(in)});
|
||||
auto ethernet = Ethernet{};
|
||||
@@ -85,6 +85,6 @@ auto Main(std::istream & in) -> void
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Part 1: " << *part1 << std::endl;
|
||||
std::cout << "Part 2: " << *part2 << std::endl;
|
||||
out << "Part 1: " << *part1 << std::endl;
|
||||
out << "Part 2: " << *part2 << std::endl;
|
||||
}
|
||||
|
@@ -135,9 +135,9 @@ TEST_SUITE("documented examples") {
|
||||
}
|
||||
}
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> 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;
|
||||
out << "Part 1: " << Part1(bugs) << std::endl;
|
||||
out << "Part 2: " << Part2(std::move(bugs), 200) << std::endl;
|
||||
}
|
||||
|
@@ -15,6 +15,7 @@ using namespace intcode;
|
||||
namespace {
|
||||
|
||||
auto RunWithIO(
|
||||
std::ostream & out,
|
||||
Machine m,
|
||||
std::string const& input
|
||||
) -> void
|
||||
@@ -25,7 +26,7 @@ auto RunWithIO(
|
||||
if (it < input.end()) { return *it++; }
|
||||
throw std::runtime_error{"insufficient input"};
|
||||
},
|
||||
[&](ValueType o) { std::cout << char(o); });
|
||||
[&](ValueType o) { out << char(o); });
|
||||
}
|
||||
|
||||
auto Search(std::string & script)
|
||||
@@ -58,7 +59,7 @@ auto Search(std::string & script)
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Main(std::istream & in) -> void
|
||||
auto Main(std::istream & in, std::ostream & out) -> void
|
||||
{
|
||||
std::string script =
|
||||
"north\n" "take sand\n"
|
||||
@@ -77,5 +78,5 @@ auto Main(std::istream & in) -> void
|
||||
|
||||
Search(script);
|
||||
|
||||
RunWithIO(Machine{ParseStream(in)}, script);
|
||||
RunWithIO(out, Machine{ParseStream(in)}, script);
|
||||
}
|
||||
|
Reference in New Issue
Block a user