This commit is contained in:
Eric Mertens 2022-11-07 21:00:14 -08:00
parent 5de6db8780
commit 719b822d5b
15 changed files with 175 additions and 26 deletions

View File

@ -26,9 +26,15 @@ target_link_libraries(day02 aocpp intcode)
add_executable(day03 day03.cpp) add_executable(day03 day03.cpp)
target_link_libraries(day03 aocpp) target_link_libraries(day03 aocpp)
add_executable(day04 day04.cpp)
target_link_libraries(day04 aocpp)
add_executable(day05 day05.cpp) add_executable(day05 day05.cpp)
target_link_libraries(day05 aocpp intcode) target_link_libraries(day05 aocpp intcode)
add_executable(day06 day06.cpp)
target_link_libraries(day06 aocpp)
add_executable(day07 day07.cpp) add_executable(day07 day07.cpp)
target_link_libraries(day07 aocpp intcode) target_link_libraries(day07 aocpp intcode)

View File

@ -8,14 +8,14 @@
#include <aocpp/Startup.hpp> #include <aocpp/Startup.hpp>
auto main(int argc, char** argv) -> int { auto main(int argc, char** argv) -> int {
auto fin = aocpp::Startup(argc, argv); auto& in = aocpp::Startup(argc, argv);
auto fuel = [](std::int64_t& x) { return x=x/3-2; }; auto fuel = [](std::int64_t& x) { return x=x/3-2; };
std::int64_t x; std::int64_t x;
std::int64_t part1 = 0; std::int64_t part1 = 0;
std::int64_t part2 = 0; std::int64_t part2 = 0;
while (fin >> x) { while (in >> x) {
part1 += fuel(x); part1 += fuel(x);
for (; x > 0; fuel(x)) { for (; x > 0; fuel(x)) {
part2 += x; part2 += x;

View File

@ -18,8 +18,7 @@ auto Compute(Machine machine, ValueType x, ValueType y) {
} // namespace } // namespace
auto main(int argc, char** argv) -> int { auto main(int argc, char** argv) -> int {
auto fin = aocpp::Startup(argc, argv); auto machine = Machine{ParseStream(aocpp::Startup(argc, argv))};
auto machine = Machine{ParseStream(fin)};
std::cout << "Part 1: " << Compute(machine, 12, 2) << std::endl; std::cout << "Part 1: " << Compute(machine, 12, 2) << std::endl;

View File

@ -66,12 +66,12 @@ auto Record (
} // namespace } // namespace
auto main(int argc, char** argv) -> int { auto main(int argc, char** argv) -> int {
auto fin = aocpp::Startup(argc, argv); auto& in = aocpp::Startup(argc, argv);
std::string line; std::string line;
std::getline(fin, line); std::getline(in, line);
auto wire0 = BuildLine(line); auto wire0 = BuildLine(line);
std::getline(fin, line); std::getline(in, line);
auto wire1 = BuildLine(line); auto wire1 = BuildLine(line);
std::map<Coord, Entry> counts; std::map<Coord, Entry> counts;

62
day04.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <tuple>
#include <fstream>
#include <iterator>
#include <vector>
#include <aocpp/Startup.hpp>
namespace {
auto Parse(std::istream & in) -> std::pair<std::int64_t, std::int64_t> {
std::int64_t lo, hi;
std::string line;
std::getline(in, line, '-');
lo = std::stol(std::move(line));
std::getline(in, line);
hi = std::stol(std::move(line));
return {lo,hi};
}
auto Valid1(std::string const& str) {
for (std::size_t i = 1; i < str.size(); i++) {
if (str[i-1] == str[i]) {
return true;
}
}
return false;
}
auto Valid2(std::string const& str) {
for (std::size_t i = 1; i < str.size(); i++) {
if ( str[i] == str[i-1] &&
(i == str.size() - 1 || str[i] != str[i+1]) &&
(i == 1 || str[i] != str[i-2]))
{
return true;
}
}
return false;
}
} // namespace
auto main(int argc, char** argv) -> int {
auto [lo,hi] = Parse(aocpp::Startup(argc, argv));
std::int64_t part1 = 0;
std::int64_t part2 = 0;
for (auto i = lo; i <= hi; i++) {
auto str = std::to_string(i);
if (std::is_sorted(str.begin(), str.end())) {
if (Valid1(str)) part1++;
if (Valid2(str)) part2++;
}
}
std::cout << "Part 1: " << part1 << std::endl;
std::cout << "Part 2: " << part2 << std::endl;
}

View File

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

81
day06.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <map>
#include <aocpp/Startup.hpp>
namespace {
auto Parse(std::istream & in) {
std::map<std::string, std::string> parents;
std::string line;
while (std::getline(in, line)) {
auto it = line.find(')');
if (it == std::string::npos) throw std::runtime_error{"bad input entry"};
parents.insert({line.substr(it+1), line.substr(0, it)});
}
return parents;
}
auto Path(std::map<std::string, std::string> const& parents, std::string const& start)
{
std::vector<std::string> path;
std::string const* name = &start;
while (*name != "COM") {
name = &parents.at(*name);
path.push_back(*name);
}
return path;
}
auto Part1(std::map<std::string, std::string> const& parents) {
std::map<std::string, std::size_t> depths {{"COM", 0}};
std::size_t part1 {0};
for (auto & [k, _] : parents) {
std::string const* cursor = &k;
std::vector<std::string const*> todo;
decltype(depths)::iterator it;
while (depths.end() == (it = depths.find(*cursor))) {
todo.push_back(cursor);
cursor = &parents.at(*cursor);
}
auto n = it->second;
for (; !todo.empty(); todo.pop_back()) {
depths[*todo.back()] = ++n;
}
part1 += n;
}
return part1;
}
auto Part2(std::map<std::string, std::string> const& parents) {
auto p1 = Path(parents, "SAN");
auto p2 = Path(parents, "YOU");
while (p1.back() == p2.back()) {
p1.pop_back();
p2.pop_back();
}
return p1.size() + p2.size();
}
} // namespace
auto main(int argc, char** argv) -> int {
auto parents = Parse(aocpp::Startup(argc, argv));
std::cout << "Part 1: " << Part1(parents) << std::endl;
std::cout << "Part 2: " << Part2(parents) << std::endl;
}

View File

@ -62,8 +62,7 @@ auto optimize(Machine machine, std::vector<ValueType> params, F f) {
} // namespace } // namespace
auto main(int argc, char** argv) -> int { auto main(int argc, char** argv) -> int {
auto fin = aocpp::Startup(argc, argv); auto machine = Machine{ParseStream(aocpp::Startup(argc, argv))};
auto machine = Machine{ParseStream(fin)};
std::cout << "Part 1: " << optimize(machine, {0, 1, 2, 3, 4}, compute1) << std::endl; std::cout << "Part 1: " << optimize(machine, {0, 1, 2, 3, 4}, compute1) << std::endl;
std::cout << "Part 2: " << optimize(std::move(machine), {5, 6, 7, 8, 9}, compute2) << std::endl; std::cout << "Part 2: " << optimize(std::move(machine), {5, 6, 7, 8, 9}, compute2) << std::endl;
} }

View File

@ -18,8 +18,7 @@ auto Compute(Machine machine, ValueType d) -> ValueType {
} // namespace } // namespace
auto main(int argc, char** argv) -> int { auto main(int argc, char** argv) -> int {
auto fin = aocpp::Startup(argc, argv); auto machine = Machine{ParseStream(aocpp::Startup(argc, argv))};
auto machine = Machine{ParseStream(fin)};
std::cout << "Part 1: " << Compute(machine, 1) << std::endl; std::cout << "Part 1: " << Compute(machine, 1) << std::endl;
std::cout << "Part 2: " << Compute(std::move(machine), 2) << std::endl; std::cout << "Part 2: " << Compute(std::move(machine), 2) << std::endl;
} }

View File

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

View File

@ -60,8 +60,7 @@ auto Compute2(Machine machine) {
} // namespace } // namespace
auto main(int argc, char** argv) -> int { auto main(int argc, char** argv) -> int {
auto fin = aocpp::Startup(argc, argv); auto machine = Machine{ParseStream(aocpp::Startup(argc, argv))};
auto machine = Machine{ParseStream(fin)};
std::cout << "Part 1: " << Compute1(machine) << std::endl; std::cout << "Part 1: " << Compute1(machine) << std::endl;
std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl; std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl;
} }

View File

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

View File

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

View File

@ -2,9 +2,12 @@
#define AOCPP_STARTUP_HPP_ #define AOCPP_STARTUP_HPP_
#include <fstream> #include <fstream>
#include <iostream>
#include <optional>
namespace aocpp { namespace aocpp {
auto Startup(int argc, char ** argv) -> std::ifstream;
auto Startup(int argc, char ** argv) -> std::istream&;
} }
#endif #endif

View File

@ -2,16 +2,21 @@
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <optional>
#include <memory>
#include <fstream> #include <fstream>
namespace aocpp { namespace aocpp {
auto Startup(int argc, char ** argv) -> std::ifstream { auto Startup(int argc, char ** argv) -> std::istream& {
if (argc != 2) { static std::ifstream fin;
std::cerr << "Expected input file argument\n"; switch (argc) {
case 2: fin = std::ifstream{argv[1]}; return fin;
case 1: return std::cin;
default:
std::cerr << "bad arguments\n";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return std::ifstream{argv[1]};
} }
} }