This commit is contained in:
Eric Mertens 2022-11-03 08:15:17 -07:00
commit 1936f63d47
9 changed files with 320 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

21
CMakeLists.txt Normal file
View File

@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)
project(aocpp19
VERSION 1
LANGUAGES C CXX
)
add_subdirectory(lib)
add_executable(day02 day02.cpp)
target_link_libraries(day02 intcode)
add_executable(day05 day05.cpp)
target_link_libraries(day05 intcode)
add_executable(day07 day07.cpp)
target_link_libraries(day07 intcode)
add_executable(day09 day09.cpp)
target_link_libraries(day09 intcode)

29
day02.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <utility>
#include <intcode.hpp>
namespace {
auto compute(intcode machine, std::int64_t x, std::int64_t y) {
machine.at(1) = x;
machine.at(2) = y;
machine.step();
return machine.at(0);
}
}
auto main() -> int {
auto machine = intcode{parse_stream(std::cin)};
std::cout << "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;
}
}
}
}

23
day05.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <iostream>
#include <utility>
#include <intcode.hpp>
namespace {
auto compute(intcode machine, std::int64_t d) -> std::int64_t {
std::int64_t last_output = -1;
while (std::visit(overloaded {
[](Halt) { return false; },
[d, &machine](Input arg) { arg.pos = d; return true; },
[&last_output](Output arg) { last_output = arg.val; return true; }
}, machine.step()));
return last_output;
}
}
auto main() -> int {
auto program = parse_stream(std::cin);
auto machine = intcode{std::move(program)};
std::cout << "Part 1: " << compute(machine, 1) << std::endl;
std::cout << "Part 2: " << compute(std::move(machine), 5) << std::endl;
}

78
day07.cpp Normal file
View File

@ -0,0 +1,78 @@
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <intcode.hpp>
namespace {
auto compute(intcode machine, std::vector<std::int64_t> const& params) -> std::int64_t {
std::vector<intcode> amps;
for (auto p : params) {
auto m = machine;
auto i = std::get<Input>(m.step());
i.pos = p;
amps.emplace_back(std::move(m));
}
std::int64_t prev = 0;
for (auto& m : amps) {
auto i = std::get<Input>(m.step());
i.pos = prev;
auto o = std::get<Output>(m.step());
prev = o.val;
}
return prev;
}
auto compute2(intcode machine, std::vector<std::int64_t> const& params) -> std::int64_t {
std::vector<intcode> amps;
for (auto p : params) {
auto m = machine;
auto i = std::get<Input>(m.step());
i.pos = p;
amps.emplace_back(std::move(m));
}
std::int64_t last = 0;
std::int64_t prev = 0;
for(;;) {
for (auto& m : amps) {
auto i = m.step();
if (auto p = std::get_if<Input>(&i)) {
p->pos = prev;
prev = std::get<Output>(m.step()).val;
} else {
return last;
}
}
last = prev;
}
}
}
auto main() -> int {
auto machine = intcode{parse_stream(std::cin)};
{
std::vector<std::int64_t> params {0,1,2,3,4};
std::int64_t best = 0;
do {
best = std::max(best, compute(machine, params));
} while (std::next_permutation(params.begin(), params.end()));
std::cout << "Part 1: " << best << std::endl;
}
{
std::vector<std::int64_t> params {5,6,7,8,9};
std::int64_t best = 0;
do {
best = std::max(best, compute2(machine, params));
} while (std::next_permutation(params.begin(), params.end()));
std::cout << "Part 2: " << best << std::endl;
}
}

25
day09.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include <utility>
#include <intcode.hpp>
namespace {
auto compute(intcode machine, std::int64_t d) -> std::int64_t {
std::int64_t output = -1;
while (std::visit(overloaded {
[](Halt) { return false; },
[d, &machine](Input arg) { arg.pos = d; return true; },
[&output](Output arg) { output = arg.val; return false; }
}, machine.step()));
return output;
}
}
auto main() -> int {
auto program = parse_stream(std::cin);
auto machine = intcode{std::move(program)};
std::cout << "Part 1: " << compute(machine, 1) << std::endl;
std::cout << "Part 2: " << compute(std::move(machine), 2) << std::endl;
}

2
lib/CMakeLists.txt Normal file
View File

@ -0,0 +1,2 @@
add_library(intcode intcode.cpp)
target_include_directories(intcode PUBLIC include)

49
lib/include/intcode.hpp Normal file
View File

@ -0,0 +1,49 @@
#ifndef INTCODE_HPP_
#define INTCODE_HPP_
#include <istream>
#include <cstddef>
#include <vector>
#include <cstdint>
#include <variant>
#include <unordered_map>
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
struct BadInstruction : public std::exception {
std::int64_t instruction;
explicit BadInstruction(std::int64_t instruction) : instruction{instruction} {}
const char* what() const noexcept override {
return "bad instruction";
}
};
struct Input {
std::int64_t& pos;
};
struct Output {
std::int64_t val;
};
struct Halt {};
class intcode {
std::vector<std::int64_t> rom_;
std::unordered_map<std::size_t, std::int64_t> ram_;
std::size_t pc_;
std::size_t base_;
auto ref(std::int64_t instruction, std::int64_t p, std::size_t offset) -> std::int64_t&;
public:
intcode();
intcode(std::vector<std::int64_t> ram);
auto step() -> std::variant<Input, Output, Halt>;
auto at(std::size_t) -> std::int64_t&;
};
auto parse_stream(std::istream& in) -> std::vector<std::int64_t>;
#endif // INTCODE_HPP_

92
lib/intcode.cpp Normal file
View File

@ -0,0 +1,92 @@
#include "intcode.hpp"
#include <iostream>
intcode::intcode() : rom_{}, ram_{}, pc_{0}, base_{0} {}
intcode::intcode(std::vector<std::int64_t> ram) : rom_{ram}, ram_{}, pc_{0}, base_{0} {}
auto intcode::ref(std::int64_t instruction, std::int64_t p, std::size_t offset) -> std::int64_t& {
auto& i = at(pc_ + offset);
switch (instruction / p % 10) {
case 0: return at(i);
case 1: return i;
case 2: return at(base_ + i);
default: throw BadInstruction{instruction};
}
}
auto intcode::at(std::size_t i) -> std::int64_t& {
return i < rom_.size() ? rom_[i] : ram_[i];
}
auto intcode::step() -> std::variant<Input, Output, Halt> {
for (;;) {
auto instruction = at(pc_);
auto a = [this,instruction]() -> std::int64_t& { return ref(instruction, 100, 1); };
auto b = [this,instruction]() -> std::int64_t& { return ref(instruction, 1000, 2); };
auto c = [this,instruction]() -> std::int64_t& { return ref(instruction, 10000, 3); };
switch (instruction % 100) {
case 1:
c() = a() + b();
pc_ += 4;
break;
case 2:
c() = a() * b();
pc_ += 4;
break;
case 3: {
auto& pos = a();
pc_ += 2;
return Input{pos};
}
case 4: {
auto val = a();
pc_ += 2;
return Output{val};
}
case 5:
pc_ = a() ? b() : pc_ + 3;
break;
case 6:
pc_ = a() ? pc_ + 3 : b();
break;
case 7:
c() = a() < b();
pc_ += 4;
break;
case 8:
c() = a() == b();
pc_ += 4;
break;
case 9:
base_ += a();
pc_ += 2;
break;
case 99:
return Halt{};
default:
throw BadInstruction{instruction};
}
}
}
auto parse_stream(std::istream& in) -> std::vector<std::int64_t> {
std::int64_t x;
std::string str;
std::vector<std::int64_t> result;
while(std::getline(in, str, ',')) {
result.push_back(std::stoi(str));
}
return result;
}