checkpoint

This commit is contained in:
Eric Mertens 2022-11-03 12:53:10 -07:00
parent 1936f63d47
commit 40f58503dc
7 changed files with 238 additions and 211 deletions

1
compile_commands.json Symbolic link
View File

@ -0,0 +1 @@
build/compile_commands.json

View File

@ -3,27 +3,29 @@
#include <intcode.hpp> #include <intcode.hpp>
using namespace intcode;
namespace { namespace {
auto compute(intcode machine, std::int64_t x, std::int64_t y) { auto compute(Machine machine, value_type x, value_type y) {
machine.at(1) = x; machine.at(1) = x;
machine.at(2) = y; machine.at(2) = y;
machine.step(); machine.step();
return machine.at(0); return machine.at(0);
} }
} } // namespace
auto main() -> int { auto main() -> int {
auto machine = intcode{parse_stream(std::cin)}; auto machine = Machine{parse_stream(std::cin)};
std::cout << "Part 1: " << compute(machine, 12, 2) << std::endl; std::cout << "Part 1: " << compute(machine, 12, 2) << std::endl;
for (std::int64_t i = 0; i < 100; i++) { for (std::int64_t i = 0; i < 100; i++) {
for (std::int64_t j = 0; j < 100; j++) { for (std::int64_t j = 0; j < 100; j++) {
if (19690720 == compute(machine, i, j)) { if (19690720 == compute(machine, i, j)) {
std::cout << "Part 2: " << 100*i+j << std::endl; std::cout << "Part 2: " << 100 * i + j << std::endl;
} }
}
} }
} }
}

View File

@ -2,22 +2,30 @@
#include <utility> #include <utility>
#include <intcode.hpp> #include <intcode.hpp>
using namespace intcode;
namespace { namespace {
auto compute(intcode machine, std::int64_t d) -> std::int64_t {
std::int64_t last_output = -1; auto compute(Machine machine, value_type d) -> value_type {
while (std::visit(overloaded { value_type last_output = -1;
[](Halt) { return false; }, while (std::visit(overloaded{
[d, &machine](Input arg) { arg.pos = d; return true; }, [](Halt) { return false; },
[&last_output](Output arg) { last_output = arg.val; return true; } [d, &machine](Input arg) {
}, machine.step())); arg.pos = d;
return last_output; return true;
} },
[&last_output](Output arg) {
last_output = arg.val;
return true;
}},
machine.step()));
return last_output;
} }
} // namespace
auto main() -> int { auto main() -> int {
auto program = parse_stream(std::cin); auto machine = Machine{parse_stream(std::cin)};
auto machine = intcode{std::move(program)}; 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; }
}

114
day07.cpp
View File

@ -1,78 +1,66 @@
#include <algorithm>
#include <concepts>
#include <iostream> #include <iostream>
#include <optional>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <algorithm>
#include <intcode.hpp> #include <intcode.hpp>
using namespace intcode;
namespace { namespace {
auto compute(intcode machine, std::vector<std::int64_t> const& params) -> std::int64_t {
std::vector<intcode> amps;
for (auto p : params) { template <class R> auto set_params(Machine machine, R const &params) {
auto m = machine; std::vector<Machine> amps;
auto i = std::get<Input>(m.step()); for (auto p : params) {
i.pos = p; auto m = machine;
amps.emplace_back(std::move(m)); std::get<Input>(m.step()).pos = p;
} amps.push_back(std::move(m));
}
std::int64_t prev = 0; return amps;
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) { template <class R>
auto m = machine; auto feed(R &&amps, value_type start) -> std::optional<value_type> {
auto i = std::get<Input>(m.step()); for (auto &m : amps) {
i.pos = p; auto i = m.step();
amps.emplace_back(std::move(m)); if (auto p = std::get_if<Input>(&i)) {
} p->pos = start;
start = std::get<Output>(m.step()).val;
std::int64_t last = 0; } else {
std::int64_t prev = 0; return {};
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;
} }
}
return {start};
} }
auto compute1(Machine machine, std::vector<value_type> const &params)
-> value_type {
return *feed(set_params(std::move(machine), params), 0);
} }
auto compute2(Machine machine, std::vector<value_type> const &params)
-> value_type {
auto amps = set_params(std::move(machine), params);
value_type last = 0;
while (auto next = feed(amps, last)) {
last = *next;
}
return last;
}
template <std::invocable<Machine, std::vector<value_type> const &> F>
auto optimize(Machine machine, std::vector<value_type> params, F f) {
value_type best = 0;
do {
best = std::max(best, f(machine, params));
} while (std::next_permutation(params.begin(), params.end()));
return best;
}
} // namespace
auto main() -> int { auto main() -> int {
auto machine = intcode{parse_stream(std::cin)}; auto machine = Machine{parse_stream(std::cin)};
{ std::cout << "Part 1: " << optimize(machine, {0, 1, 2, 3, 4}, compute1) << std::endl;
std::vector<std::int64_t> params {0,1,2,3,4}; std::cout << "Part 2: " << optimize(std::move(machine), {5, 6, 7, 8, 9}, compute2) << std::endl;
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;
}
}

View File

@ -2,24 +2,30 @@
#include <utility> #include <utility>
#include <intcode.hpp> #include <intcode.hpp>
using namespace intcode;
namespace { namespace {
auto compute(intcode machine, std::int64_t d) -> std::int64_t { auto compute(Machine machine, value_type d) -> value_type {
std::int64_t output = -1; value_type output = -1;
while (std::visit(overloaded { while (std::visit(overloaded{
[](Halt) { return false; }, [](Halt) { return false; },
[d, &machine](Input arg) { arg.pos = d; return true; }, [d](Input arg) {
[&output](Output arg) { output = arg.val; return false; } arg.pos = d;
}, machine.step())); return true;
return output; },
[&output](Output arg) {
output = arg.val;
return false;
}},
machine.step()));
return output;
} }
} } // namespace
auto main() -> int { auto main() -> int {
auto program = parse_stream(std::cin); auto machine = Machine{parse_stream(std::cin)};
auto machine = intcode{std::move(program)}; 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

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

View File

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