15
This commit is contained in:
parent
7018a2998a
commit
e88863ed37
|
@ -22,3 +22,9 @@ target_link_libraries(day09 intcode)
|
|||
|
||||
add_executable(day11 day11.cpp)
|
||||
target_link_libraries(day11 intcode)
|
||||
|
||||
add_executable(day13 day13.cpp)
|
||||
target_link_libraries(day13 intcode)
|
||||
|
||||
add_executable(day15 day15.cpp)
|
||||
target_link_libraries(day15 intcode)
|
||||
|
|
70
day13.cpp
Normal file
70
day13.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <tuple>
|
||||
#include <set>
|
||||
|
||||
#include <intcode.hpp>
|
||||
using namespace intcode;
|
||||
|
||||
namespace {
|
||||
|
||||
using Coord = std::pair<ValueType, ValueType>;
|
||||
|
||||
auto Compute1(Machine machine)
|
||||
-> std::size_t {
|
||||
std::set<Coord> screen;
|
||||
|
||||
for (;;) {
|
||||
auto effect = machine.Step();
|
||||
if (std::holds_alternative<Halt>(effect)) {
|
||||
return screen.size();
|
||||
}
|
||||
auto x = std::get<Output>(effect).val;
|
||||
auto y = std::get<Output>(machine.Step()).val;
|
||||
auto v = std::get<Output>(machine.Step()).val;
|
||||
|
||||
if (2 == v) { screen.insert({x,y}); } else { screen.erase({x,y}); }
|
||||
}
|
||||
}
|
||||
|
||||
auto Compute2(Machine machine) {
|
||||
ValueType score {0};
|
||||
ValueType paddleX {0};
|
||||
ValueType ballX {0};
|
||||
|
||||
machine.At(0) = 2;
|
||||
for(;;) {
|
||||
auto effect = machine.Step();
|
||||
if (std::holds_alternative<Halt>(effect)) {
|
||||
return score;
|
||||
} else if (auto i = std::get_if<Input>(&effect)) {
|
||||
i->pos = paddleX < ballX ? 1
|
||||
: paddleX > ballX ? -1 : 0;
|
||||
} else {
|
||||
auto x = std::get<Output>(effect).val;
|
||||
auto y = std::get<Output>(machine.Step()).val;
|
||||
auto v = std::get<Output>(machine.Step()).val;
|
||||
|
||||
if (-1 == x && 0 == y) {
|
||||
score = v;
|
||||
} else {
|
||||
switch (v) {
|
||||
case 3: // paddle
|
||||
paddleX = x;
|
||||
break;
|
||||
case 4: // ball
|
||||
ballX = x;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
auto main() -> int {
|
||||
auto machine = Machine{ParseStream(std::cin)};
|
||||
std::cout << "Part 1: " << Compute1(machine) << std::endl;
|
||||
std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl;
|
||||
}
|
65
day15.cpp
Normal file
65
day15.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include <iterator>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <tuple>
|
||||
#include <map>
|
||||
#include <deque>
|
||||
|
||||
#include <intcode.hpp>
|
||||
using namespace intcode;
|
||||
|
||||
namespace {
|
||||
|
||||
using Coord = std::pair<ValueType, ValueType>;
|
||||
|
||||
auto Compute(Machine machine) -> std::pair<int,int> {
|
||||
int part1 = 0;
|
||||
int part2 = 0;
|
||||
|
||||
Coord here {0,0};
|
||||
std::map<Coord, ValueType> world;
|
||||
|
||||
std::deque<std::tuple<int, Coord, Machine>> todo {{0, {0,0}, std::move(machine)}};
|
||||
|
||||
auto action = [&world, &todo](Machine m, int steps, ValueType cmd, Coord coord) {
|
||||
if (!world.contains(coord)) {
|
||||
std::get<Input>(m.Step()).pos = cmd;
|
||||
auto o = std::get<Output>(m.Step()).val;
|
||||
world[coord] = o;
|
||||
if (o) {
|
||||
todo.emplace_back(steps + 1, coord, std::move(m));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
while (!todo.empty()) {
|
||||
auto [steps, here, machine] = todo.front();
|
||||
auto [x,y] = here;
|
||||
todo.pop_front();
|
||||
|
||||
if (2 == world[here]) {
|
||||
part1 = steps;
|
||||
todo.clear();
|
||||
world.clear();
|
||||
world[here] = 2;
|
||||
steps = 0;
|
||||
part2 = 0;
|
||||
}
|
||||
part2 = std::max(part2, steps);
|
||||
|
||||
action(machine, steps, 1, {x,y-1});
|
||||
action(machine, steps, 2, {x,y+1});
|
||||
action(machine, steps, 3, {x-1,y});
|
||||
action(std::move(machine), steps, 4, {x+1,y});
|
||||
}
|
||||
return {part1,part2};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
auto main() -> int {
|
||||
auto machine = Machine{ParseStream(std::cin)};
|
||||
auto [part1, part2] = Compute(machine);
|
||||
std::cout << "Part 1: " << part1 << std::endl;
|
||||
std::cout << "Part 2: " << part2 << std::endl;
|
||||
}
|
Loading…
Reference in New Issue
Block a user