diff --git a/CMakeLists.txt b/CMakeLists.txt index b9361eb..093b595 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,27 +15,31 @@ else() endif() add_subdirectory(lib) +add_subdirectory(intcode) + +add_executable(day01 day01.cpp) +target_link_libraries(day01 aocpp) add_executable(day02 day02.cpp) -target_link_libraries(day02 intcode) +target_link_libraries(day02 aocpp intcode) add_executable(day05 day05.cpp) -target_link_libraries(day05 intcode) +target_link_libraries(day05 aocpp intcode) add_executable(day07 day07.cpp) -target_link_libraries(day07 intcode) +target_link_libraries(day07 aocpp intcode) add_executable(day09 day09.cpp) -target_link_libraries(day09 intcode) +target_link_libraries(day09 aocpp intcode) add_executable(day11 day11.cpp) -target_link_libraries(day11 intcode) +target_link_libraries(day11 aocpp intcode) add_executable(day13 day13.cpp) -target_link_libraries(day13 intcode) +target_link_libraries(day13 aocpp intcode) add_executable(day15 day15.cpp) -target_link_libraries(day15 intcode) +target_link_libraries(day15 aocpp intcode) add_executable(day23 day23.cpp) -target_link_libraries(day23 intcode) +target_link_libraries(day23 aocpp intcode) diff --git a/day01.cpp b/day01.cpp new file mode 100644 index 0000000..624630d --- /dev/null +++ b/day01.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +auto main(int argc, char** argv) -> int { + auto fin = aocpp::Startup(argc, argv); + std::vector input; + std::copy(std::istream_iterator{fin}, + std::istream_iterator{}, + std::back_inserter(input)); + + auto fuel = [](std::int64_t& x) { return x=x/3-2; }; + + std::int64_t part1 = 0; + std::int64_t part2 = 0; + + for (auto x : input) { + part1 += fuel(x); + for (; x > 0; fuel(x)) { + part2 += x; + } + } + std::cout << "Part 1: " << part1 << std::endl; + std::cout << "Part 2: " << part2 << std::endl; +} diff --git a/day02.cpp b/day02.cpp index cacfcca..eca052f 100644 --- a/day02.cpp +++ b/day02.cpp @@ -1,6 +1,7 @@ #include #include +#include #include using namespace intcode; @@ -17,7 +18,8 @@ auto Compute(Machine machine, ValueType x, ValueType y) { } // namespace auto main(int argc, char** argv) -> int { - auto machine = Startup(argc, argv); + auto fin = aocpp::Startup(argc, argv); + auto machine = Machine{ParseStream(fin)}; std::cout << "Part 1: " << Compute(machine, 12, 2) << std::endl; diff --git a/day05.cpp b/day05.cpp index 6885cfc..19609b1 100644 --- a/day05.cpp +++ b/day05.cpp @@ -1,6 +1,7 @@ #include #include +#include #include using namespace intcode; @@ -17,7 +18,8 @@ auto Compute(Machine machine, ValueType d) -> ValueType { } // namespace auto main(int argc, char** argv) -> int { - auto machine = Startup(argc, argv); + auto fin = aocpp::Startup(argc, argv); + auto machine = Machine{ParseStream(fin)}; std::cout << "Part 1: " << Compute(machine, 1) << std::endl; std::cout << "Part 2: " << Compute(std::move(machine), 5) << std::endl; } diff --git a/day07.cpp b/day07.cpp index 59e39a4..2b2c3b1 100644 --- a/day07.cpp +++ b/day07.cpp @@ -5,16 +5,19 @@ #include #include +#include #include using namespace intcode; namespace { template auto Controller(Machine machine, R const ¶ms) { + auto& i = std::get(Step(machine)).input; std::vector amps; + amps.reserve(params.size()); for (auto p : params) { + i = p; amps.push_back(machine); - StepInput(amps.back(), p); } return amps; } @@ -59,7 +62,8 @@ auto optimize(Machine machine, std::vector params, F f) { } // namespace auto main(int argc, char** argv) -> int { - auto machine = Startup(argc, argv); + auto fin = 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 2: " << optimize(std::move(machine), {5, 6, 7, 8, 9}, compute2) << std::endl; } diff --git a/day09.cpp b/day09.cpp index 0823756..f59db69 100644 --- a/day09.cpp +++ b/day09.cpp @@ -1,6 +1,7 @@ #include #include +#include #include using namespace intcode; @@ -17,7 +18,8 @@ auto Compute(Machine machine, ValueType d) -> ValueType { } // namespace auto main(int argc, char** argv) -> int { - auto machine = Startup(argc, argv); + auto fin = aocpp::Startup(argc, argv); + auto machine = Machine{ParseStream(fin)}; std::cout << "Part 1: " << Compute(machine, 1) << std::endl; std::cout << "Part 2: " << Compute(std::move(machine), 2) << std::endl; } diff --git a/day11.cpp b/day11.cpp index 8db1ad2..2455bad 100644 --- a/day11.cpp +++ b/day11.cpp @@ -3,8 +3,10 @@ #include #include +#include +#include #include -#include +using namespace aocpp; using namespace intcode; namespace { @@ -36,7 +38,8 @@ auto Compute(Machine machine, ValueType start) } // namespace auto main(int argc, char** argv) -> int { - auto machine = Startup(argc, argv); + auto fin = aocpp::Startup(argc, argv); + auto machine = Machine{ParseStream(fin)}; std::cout << "Part 1: " << Compute(machine, 0).size() << "\nPart 2\n"; Draw(std::cout, Compute(std::move(machine), 1)); } diff --git a/day13.cpp b/day13.cpp index e1b5d3d..b90f301 100644 --- a/day13.cpp +++ b/day13.cpp @@ -4,8 +4,10 @@ #include #include +#include +#include #include -#include +using namespace aocpp; using namespace intcode; namespace { @@ -58,7 +60,8 @@ auto Compute2(Machine machine) { } // namespace auto main(int argc, char** argv) -> int { - auto machine = Startup(argc, argv); + auto fin = aocpp::Startup(argc, argv); + auto machine = Machine{ParseStream(fin)}; std::cout << "Part 1: " << Compute1(machine) << std::endl; std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl; } diff --git a/day15.cpp b/day15.cpp index eac9837..9f290bc 100644 --- a/day15.cpp +++ b/day15.cpp @@ -6,8 +6,10 @@ #include #include +#include +#include #include -#include +using namespace aocpp; using namespace intcode; namespace { @@ -89,7 +91,8 @@ auto Compute(std::map world) -> std::pair { } // namespace auto main(int argc, char** argv) -> int { - auto [p1,p2] = Compute(Explore(Startup(argc, argv))); + auto fin = aocpp::Startup(argc, argv); + auto [p1,p2] = Compute(Explore(Machine{ParseStream(fin)})); std::cout << "Part 1: " << p1 << std::endl; std::cout << "Part 2: " << p2 << std::endl; } diff --git a/day23.cpp b/day23.cpp index a1f7eb5..a952965 100644 --- a/day23.cpp +++ b/day23.cpp @@ -2,12 +2,11 @@ #include #include #include -#include #include #include #include -#include +#include #include using namespace intcode; @@ -19,10 +18,13 @@ using Ethernet = std::deque; auto BuildNetwork(Machine m) -> std::vector { std::vector machines; + machines.reserve(50); + auto& i = std::get(Step(m)).input; for (i = 0; i < 50; i++) { machines.push_back(m); } + return machines; } @@ -55,7 +57,8 @@ auto Interact(Ethernet & ethernet, Machine & m, std::optional p) -> voi } // namespace auto main(int argc, char** argv) -> int { - auto machines = BuildNetwork(Startup(argc, argv)); + auto fin = aocpp::Startup(argc, argv); + auto machines = BuildNetwork(Machine{ParseStream(fin)}); auto ethernet = Ethernet{}; std::optional part1; diff --git a/intcode/CMakeLists.txt b/intcode/CMakeLists.txt new file mode 100644 index 0000000..e10e3fc --- /dev/null +++ b/intcode/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(intcode src/Parser.cpp src/Interpreter.cpp src/Machine.cpp) +target_include_directories(intcode PUBLIC include) diff --git a/lib/include/Overload.hpp b/intcode/include/Overload.hpp similarity index 100% rename from lib/include/Overload.hpp rename to intcode/include/Overload.hpp diff --git a/lib/include/intcode/Interpreter.hpp b/intcode/include/intcode/Interpreter.hpp similarity index 100% rename from lib/include/intcode/Interpreter.hpp rename to intcode/include/intcode/Interpreter.hpp diff --git a/lib/include/intcode/Machine.hpp b/intcode/include/intcode/Machine.hpp similarity index 82% rename from lib/include/intcode/Machine.hpp rename to intcode/include/intcode/Machine.hpp index 2066f98..7cd100a 100644 --- a/lib/include/intcode/Machine.hpp +++ b/intcode/include/intcode/Machine.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace intcode { @@ -17,7 +18,10 @@ class Machine { public: Machine(); - explicit Machine(std::vector ram); + + /// Initialize a new machine with a given program starting + /// execution and program counter 0. + explicit Machine(std::vector program); /// Access memory at absolute address /// @param address diff --git a/lib/include/intcode/Parser.hpp b/intcode/include/intcode/Parser.hpp similarity index 100% rename from lib/include/intcode/Parser.hpp rename to intcode/include/intcode/Parser.hpp diff --git a/lib/include/intcode/intcode.hpp b/intcode/include/intcode/intcode.hpp similarity index 85% rename from lib/include/intcode/intcode.hpp rename to intcode/include/intcode/intcode.hpp index 44cd87f..fb11a8f 100644 --- a/lib/include/intcode/intcode.hpp +++ b/intcode/include/intcode/intcode.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #endif diff --git a/lib/src/Interpreter.cpp b/intcode/src/Interpreter.cpp similarity index 100% rename from lib/src/Interpreter.cpp rename to intcode/src/Interpreter.cpp diff --git a/lib/src/Machine.cpp b/intcode/src/Machine.cpp similarity index 83% rename from lib/src/Machine.cpp rename to intcode/src/Machine.cpp index e7737cf..9e2f56f 100644 --- a/lib/src/Machine.cpp +++ b/intcode/src/Machine.cpp @@ -4,8 +4,8 @@ namespace intcode { Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {} -Machine::Machine(std::vector ram) - : rom_{ram}, ram_{}, pc_{0}, base_{0} {} +Machine::Machine(std::vector program) + : rom_{program}, ram_{}, pc_{0}, base_{0} {} auto Machine::At(std::size_t i) -> ValueType & { return i < rom_.size() ? rom_[i] : ram_[i]; diff --git a/lib/src/Parser.cpp b/intcode/src/Parser.cpp similarity index 100% rename from lib/src/Parser.cpp rename to intcode/src/Parser.cpp diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 93ec6be..6fb1e73 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,2 +1,2 @@ -add_library(intcode src/Coord.cpp src/Parser.cpp src/Interpreter.cpp src/Machine.cpp src/Startup.cpp) -target_include_directories(intcode PUBLIC include) +add_library(aocpp src/Startup.cpp src/Coord.cpp) +target_include_directories(aocpp PUBLIC include) \ No newline at end of file diff --git a/lib/include/intcode/Coord.hpp b/lib/include/aocpp/Coord.hpp similarity index 80% rename from lib/include/intcode/Coord.hpp rename to lib/include/aocpp/Coord.hpp index 712b9be..0eb4bf4 100644 --- a/lib/include/intcode/Coord.hpp +++ b/lib/include/aocpp/Coord.hpp @@ -1,14 +1,12 @@ -#ifndef INTCODE_COORD_HPP_ -#define INTCODE_COORD_HPP_ +#ifndef AOCPP_COORD_HPP_ +#define AOCPP_COORD_HPP_ #include #include #include #include -#include - -namespace intcode { +namespace aocpp { /// Cartesian coordinate /// @@ -19,7 +17,7 @@ struct Coord { }; -auto Draw(std::ostream & out, std::map image) -> void; +auto Draw(std::ostream & out, std::map image) -> void; /// Move one unit up auto Up(Coord) -> Coord; @@ -60,9 +58,9 @@ auto operator<<(std::ostream &, Coord) -> std::ostream &; } // namespace template<> -struct std::hash +struct std::hash { - auto operator()(intcode::Coord const& c) const noexcept -> std::size_t { + auto operator()(aocpp::Coord const& c) const noexcept -> std::size_t { std::hash h; return h(c.x) ^ h(c.y); } diff --git a/lib/include/aocpp/Startup.hpp b/lib/include/aocpp/Startup.hpp new file mode 100644 index 0000000..c527c24 --- /dev/null +++ b/lib/include/aocpp/Startup.hpp @@ -0,0 +1,10 @@ +#ifndef AOCPP_STARTUP_HPP_ +#define AOCPP_STARTUP_HPP_ + +#include + +namespace aocpp { +auto Startup(int argc, char ** argv) -> std::ifstream; +} + +#endif diff --git a/lib/src/Coord.cpp b/lib/src/Coord.cpp index 9ba63f6..992a248 100644 --- a/lib/src/Coord.cpp +++ b/lib/src/Coord.cpp @@ -1,9 +1,9 @@ -#include +#include -namespace intcode { +namespace aocpp { -auto Draw(std::ostream & out, std::map image) -> void { - ValueType minX = 0, maxX = 0, minY = 0, maxY = 0; +auto Draw(std::ostream & out, std::map image) -> void { + std::int64_t minX = 0, maxX = 0, minY = 0, maxY = 0; for (auto&& [k, _] : image) { auto [x,y] = k; minX = std::min(minX, x); @@ -11,8 +11,8 @@ auto Draw(std::ostream & out, std::map image) -> void { minY = std::min(minY, y); maxY = std::max(maxY, y); } - for (ValueType y = minY; y <= maxY; y++) { - for (ValueType x = minX; x <= maxX; x++) { + for (std::int64_t y = minY; y <= maxY; y++) { + for (std::int64_t x = minX; x <= maxX; x++) { auto it = image.find({x,y}); if (image.end() == it) { out << "░"; diff --git a/lib/src/Startup.cpp b/lib/src/Startup.cpp new file mode 100644 index 0000000..c12197c --- /dev/null +++ b/lib/src/Startup.cpp @@ -0,0 +1,17 @@ +#include + +#include +#include +#include + +namespace aocpp { + +auto Startup(int argc, char ** argv) -> std::ifstream { + if (argc != 2) { + std::cerr << "Expected input file argument\n"; + exit(EXIT_FAILURE); + } + return std::ifstream{argv[1]}; +} + +}