make a common lib for non-intcode problems
This commit is contained in:
parent
041c055f43
commit
ebb068cfc5
|
@ -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)
|
||||
|
|
33
day01.cpp
Normal file
33
day01.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
|
||||
auto main(int argc, char** argv) -> int {
|
||||
auto fin = aocpp::Startup(argc, argv);
|
||||
std::vector<std::int64_t> input;
|
||||
std::copy(std::istream_iterator<std::int64_t>{fin},
|
||||
std::istream_iterator<std::int64_t>{},
|
||||
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;
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <intcode/intcode.hpp>
|
||||
|
||||
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;
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <intcode/intcode.hpp>
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -5,16 +5,19 @@
|
|||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <intcode/intcode.hpp>
|
||||
using namespace intcode;
|
||||
|
||||
namespace {
|
||||
|
||||
template <class R> auto Controller(Machine machine, R const ¶ms) {
|
||||
auto& i = std::get<Input>(Step(machine)).input;
|
||||
std::vector<Machine> 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<ValueType> 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;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <intcode/intcode.hpp>
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -3,8 +3,10 @@
|
|||
#include <tuple>
|
||||
#include <map>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <aocpp/Coord.hpp>
|
||||
#include <intcode/intcode.hpp>
|
||||
#include <intcode/Coord.hpp>
|
||||
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));
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@
|
|||
#include <tuple>
|
||||
#include <set>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <aocpp/Coord.hpp>
|
||||
#include <intcode/intcode.hpp>
|
||||
#include <intcode/Coord.hpp>
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -6,8 +6,10 @@
|
|||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <aocpp/Coord.hpp>
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <intcode/intcode.hpp>
|
||||
#include <intcode/Coord.hpp>
|
||||
using namespace aocpp;
|
||||
using namespace intcode;
|
||||
|
||||
namespace {
|
||||
|
@ -89,7 +91,8 @@ auto Compute(std::map<Coord, ValueType> world) -> std::pair<int, int> {
|
|||
} // 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;
|
||||
}
|
||||
|
|
|
@ -2,12 +2,11 @@
|
|||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <intcode/intcode.hpp>
|
||||
using namespace intcode;
|
||||
|
||||
|
@ -19,10 +18,13 @@ using Ethernet = std::deque<Packet>;
|
|||
|
||||
auto BuildNetwork(Machine m) -> std::vector<Machine> {
|
||||
std::vector<Machine> machines;
|
||||
machines.reserve(50);
|
||||
|
||||
auto& i = std::get<Input>(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<Payload> 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<ValueType> part1;
|
||||
|
|
2
intcode/CMakeLists.txt
Normal file
2
intcode/CMakeLists.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
add_library(intcode src/Parser.cpp src/Interpreter.cpp src/Machine.cpp)
|
||||
target_include_directories(intcode PUBLIC include)
|
|
@ -4,6 +4,7 @@
|
|||
#include <cstddef>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
namespace intcode {
|
||||
|
||||
|
@ -17,7 +18,10 @@ class Machine {
|
|||
|
||||
public:
|
||||
Machine();
|
||||
explicit Machine(std::vector<ValueType> ram);
|
||||
|
||||
/// Initialize a new machine with a given program starting
|
||||
/// execution and program counter 0.
|
||||
explicit Machine(std::vector<ValueType> program);
|
||||
|
||||
/// Access memory at absolute address
|
||||
/// @param address
|
|
@ -4,7 +4,6 @@
|
|||
#include <intcode/Interpreter.hpp>
|
||||
#include <intcode/Machine.hpp>
|
||||
#include <intcode/Parser.hpp>
|
||||
#include <intcode/Startup.hpp>
|
||||
#include <Overload.hpp>
|
||||
|
||||
#endif
|
|
@ -4,8 +4,8 @@ namespace intcode {
|
|||
|
||||
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
|
||||
|
||||
Machine::Machine(std::vector<ValueType> ram)
|
||||
: rom_{ram}, ram_{}, pc_{0}, base_{0} {}
|
||||
Machine::Machine(std::vector<ValueType> program)
|
||||
: rom_{program}, ram_{}, pc_{0}, base_{0} {}
|
||||
|
||||
auto Machine::At(std::size_t i) -> ValueType & {
|
||||
return i < rom_.size() ? rom_[i] : ram_[i];
|
|
@ -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)
|
|
@ -1,14 +1,12 @@
|
|||
#ifndef INTCODE_COORD_HPP_
|
||||
#define INTCODE_COORD_HPP_
|
||||
#ifndef AOCPP_COORD_HPP_
|
||||
#define AOCPP_COORD_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
|
||||
#include <intcode/Machine.hpp>
|
||||
|
||||
namespace intcode {
|
||||
namespace aocpp {
|
||||
|
||||
/// Cartesian coordinate
|
||||
///
|
||||
|
@ -19,7 +17,7 @@ struct Coord {
|
|||
};
|
||||
|
||||
|
||||
auto Draw(std::ostream & out, std::map<Coord, ValueType> image) -> void;
|
||||
auto Draw(std::ostream & out, std::map<Coord, std::int64_t> 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<intcode::Coord>
|
||||
struct std::hash<aocpp::Coord>
|
||||
{
|
||||
auto operator()(intcode::Coord const& c) const noexcept -> std::size_t {
|
||||
auto operator()(aocpp::Coord const& c) const noexcept -> std::size_t {
|
||||
std::hash<std::int64_t> h;
|
||||
return h(c.x) ^ h(c.y);
|
||||
}
|
10
lib/include/aocpp/Startup.hpp
Normal file
10
lib/include/aocpp/Startup.hpp
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef AOCPP_STARTUP_HPP_
|
||||
#define AOCPP_STARTUP_HPP_
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace aocpp {
|
||||
auto Startup(int argc, char ** argv) -> std::ifstream;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,9 +1,9 @@
|
|||
#include <intcode/Coord.hpp>
|
||||
#include <aocpp/Coord.hpp>
|
||||
|
||||
namespace intcode {
|
||||
namespace aocpp {
|
||||
|
||||
auto Draw(std::ostream & out, std::map<Coord, ValueType> image) -> void {
|
||||
ValueType minX = 0, maxX = 0, minY = 0, maxY = 0;
|
||||
auto Draw(std::ostream & out, std::map<Coord, std::int64_t> 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<Coord, ValueType> 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 << "░";
|
||||
|
|
17
lib/src/Startup.cpp
Normal file
17
lib/src/Startup.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include <aocpp/Startup.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
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]};
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user