This commit is contained in:
Eric Mertens 2022-11-04 17:39:35 -07:00
parent 8c6d8d4a29
commit 29a840bdfc
6 changed files with 57 additions and 91 deletions

View File

@ -6,6 +6,14 @@ project(aocpp19
LANGUAGES C CXX LANGUAGES C CXX
) )
include(CheckIPOSupported)
check_ipo_supported(RESULT result OUTPUT output)
if(result)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO is not supported: ${output}")
endif()
add_subdirectory(lib) add_subdirectory(lib)
add_executable(day02 day02.cpp) add_executable(day02 day02.cpp)

View File

@ -4,19 +4,17 @@
#include <map> #include <map>
#include <intcode/intcode.hpp> #include <intcode/intcode.hpp>
#include <intcode/Grid.hpp>
using namespace intcode; using namespace intcode;
namespace { namespace {
using Coord = std::pair<ValueType, ValueType>;
auto Compute(Machine machine, ValueType start) auto Compute(Machine machine, ValueType start)
-> std::map<Coord, ValueType> { -> std::map<Coord, ValueType> {
Coord here {0,0}; Coord here {0,0};
std::map<Coord, ValueType> paint {{here,start}}; std::map<Coord, ValueType> paint {{here,start}};
ValueType dx {0}; Coord dir {0, -1};
ValueType dy {-1};
bool next_color = true; bool next_color = true;
@ -26,10 +24,8 @@ auto Compute(Machine machine, ValueType start)
if (next_color) { if (next_color) {
paint[here] = o; paint[here] = o;
} else { } else {
std::swap(dx, dy); dir = (o ? CW : CCW)(dir);
(o ? dx : dy) *= -1; here = Add(here, dir);
here.first += dx;
here.second += dy;
} }
next_color = !next_color; next_color = !next_color;
}); });
@ -37,23 +33,6 @@ auto Compute(Machine machine, ValueType start)
return paint; return paint;
} }
auto Draw(std::ostream & out, std::map<Coord, ValueType> image) {
ValueType minX = 0, maxX = 0, minY = 0, maxY = 0;
for (auto&& [k, _] : image) {
auto [x,y] = k;
minX = std::min(minX, x);
maxX = std::max(maxX, x);
minY = std::min(minY, y);
maxY = std::max(maxY, y);
}
for (ValueType y = minY; y <= maxY; y++) {
for (ValueType x = minX; x <= maxX; x++) {
out << (image[{x,y}] ? "" : "");
}
out << "\n";
}
}
} // namespace } // namespace
auto main() -> int { auto main() -> int {

View File

@ -4,12 +4,11 @@
#include <set> #include <set>
#include <intcode/intcode.hpp> #include <intcode/intcode.hpp>
#include <intcode/Grid.hpp>
using namespace intcode; using namespace intcode;
namespace { namespace {
using Coord = std::pair<ValueType, ValueType>;
auto Compute1(Machine machine) auto Compute1(Machine machine)
-> std::size_t { -> std::size_t {
std::set<Coord> screen; std::set<Coord> screen;

104
day15.cpp
View File

@ -1,85 +1,65 @@
#include <iterator>
#include <iostream>
#include <utility>
#include <tuple>
#include <map>
#include <deque> #include <deque>
#include <vector> #include <iostream>
#include <set>
#include <tuple>
#include <utility>
#include <intcode/intcode.hpp> #include <intcode/intcode.hpp>
#include <intcode/Grid.hpp>
using namespace intcode; using namespace intcode;
namespace { namespace {
using Coord = std::pair<ValueType, ValueType>; std::pair<ValueType, Coord(*)(Coord)> const moves[4]
{{1, Up}, {2, Down}, {3, Left}, {4, Right}};
auto Compute(Machine machine) -> std::pair<int,int> { auto Fork(Machine m, ValueType cmd) {
Coord here {0,0}; std::get<Input>(Step(m)).pos = cmd;
std::map<Coord, ValueType> world {{{0,0},1}}; auto o = std::get<Output>(Step(m)).val;
return std::make_pair(o, std::move(m));
}
using State = std::tuple<Coord, Machine>; auto Compute(Machine machine) {
Coord const origin {0,0};
std::set<Coord> world {origin};
std::vector<State> layer {{{0,0}, std::move(machine)}}; std::deque<std::tuple<int, Coord, Machine>> todo
std::vector<State> next_layer; {{0, origin, std::move(machine)}};
auto action = [&](Machine m, ValueType cmd, Coord coord) { long part1 = -1;
if (!world.contains(coord)) { long part2 = -1;
std::get<Input>(Step(m)).pos = cmd;
auto o = std::get<Output>(Step(m)).val; top:
world[coord] = o; while (!todo.empty()) {
if (o) { auto & [steps, loc, m] = todo.front();
next_layer.emplace_back(coord, std::move(m)); part2 = steps;
steps++;
for (auto&& [cmd, fun] : moves) {
auto loc_ = fun(loc);
if (world.insert(loc_).second) {
auto [o,m_] = Fork(m, cmd);
switch (o) {
case 2:
part1 = steps;
world = {origin};
todo = {{0, origin, std::move(m_)}};
goto top;
case 1:
todo.emplace_back(steps, loc_, std::move(m_));
}
} }
} }
}; todo.pop_front();
int part1 = 0;
while (!layer.empty()) {
for (auto && s : layer) {
auto& [loc, m] = s;
if (2 == world[loc]) {
world.clear();
world[loc] = 2;
auto start = std::move(m);
layer.clear();
layer.push_back({loc, std::move(start)});
goto loop2;
}
auto [x,y] = loc;
action(m, 1, {x,y-1});
action(m, 2, {x,y+1});
action(m, 3, {x-1,y});
action(std::move(m), 4, {x+1,y});
}
layer = std::move(next_layer);
next_layer.clear();
part1++;
} }
return std::make_pair(part1,part2);
loop2:
int part2 = 0;
while (!layer.empty()) {
for (auto && s : layer) {
auto& [loc, m] = s;
auto [x,y] = loc;
action(m, 1, {x,y-1});
action(m, 2, {x,y+1});
action(m, 3, {x-1,y});
action(std::move(m), 4, {x+1,y});
}
layer = std::move(next_layer);
next_layer.clear();
part2++;
}
return {part1, part2-1};
} }
} // namespace } // namespace
auto main() -> int { auto main() -> int {
auto machine = Machine{ParseStream(std::cin)}; auto [part1, part2] = Compute(Machine{ParseStream(std::cin)});
auto [part1, part2] = Compute(machine);
std::cout << "Part 1: " << part1 << std::endl; std::cout << "Part 1: " << part1 << std::endl;
std::cout << "Part 2: " << part2 << std::endl; std::cout << "Part 2: " << part2 << std::endl;
} }

View File

@ -1,2 +1,2 @@
add_library(intcode src/Parser.cpp src/Interpreter.cpp src/Machine.cpp) add_library(intcode src/Grid.cpp src/Parser.cpp src/Interpreter.cpp src/Machine.cpp)
target_include_directories(intcode PUBLIC include) target_include_directories(intcode PUBLIC include)

View File

@ -1,9 +1,9 @@
#ifndef INTCODE_INTCODE_HPP_ #ifndef INTCODE_INTCODE_HPP_
#define INTCODE_INTCODE_HPP_ #define INTCODE_INTCODE_HPP_
#include <intcode/Interpreter.hpp>
#include <intcode/Machine.hpp> #include <intcode/Machine.hpp>
#include <intcode/Parser.hpp> #include <intcode/Parser.hpp>
#include <intcode/Interpreter.hpp>
#include <Overload.hpp> #include <Overload.hpp>
#endif #endif