aocpp/2018/15.cpp

487 lines
11 KiB
C++
Raw Normal View History

2022-11-26 10:01:10 -08:00
#include <cstdint>
#include <iostream>
#include <map>
2022-11-26 22:32:57 -08:00
#include <numeric>
#include <optional>
#include <set>
2022-11-26 10:01:10 -08:00
#include <sstream>
#include <string>
#include <tuple>
2022-11-27 11:01:31 -08:00
#include <queue>
2022-11-26 10:01:10 -08:00
#include <doctest.h>
#include <aocpp/Startup.hpp>
#include <aocpp/Grid.hpp>
using namespace aocpp;
namespace {
auto IsUnit(char c) -> bool {
return c == 'G' || c == 'E';
}
auto IsOpposed(char x, char y) -> bool {
return x == 'G' && y == 'E' || x == 'E' && y == 'G';
}
2022-11-26 22:32:57 -08:00
struct IsBefore {
auto operator()(Coord const& a, Coord const& b) const -> bool {
return a.y < b.y || a.y == b.y && a.x < b.x;
}
};
int const initial_hp = 200;
Coord const reading_order[] {{0,-1}, {-1,0}, {1,0}, {0,1}};
struct Game {
Grid grid_;
2022-11-27 10:41:24 -08:00
std::map<Coord, int> units_;
2022-11-26 22:32:57 -08:00
int e_damage_;
int g_damage_;
bool no_elves_can_die;
std::ostream * debug_out_;
2022-11-27 10:41:24 -08:00
Game(Grid grid);
auto Simulate() -> std::optional<int>;
auto PickTarget(Coord my_coord, char my_team)
-> std::map<Coord, int>::iterator;
auto Move(Coord my_coord, char my_team) -> std::optional<Coord>;
};
Game::Game(Grid grid)
2022-11-26 22:32:57 -08:00
: grid_(std::move(grid))
2022-11-27 10:41:24 -08:00
, units_{}
2022-11-26 22:32:57 -08:00
, e_damage_(3)
, g_damage_(3)
, no_elves_can_die{false}
, debug_out_{}
{
// set each unit to its initial hit points
2024-09-05 20:10:34 -07:00
for (auto [k, v] : grid_) {
2022-11-26 22:32:57 -08:00
if (IsUnit(v)) {
2022-11-27 10:41:24 -08:00
units_[k] = initial_hp;
2022-11-26 22:32:57 -08:00
}
2024-09-05 20:10:34 -07:00
}
2022-11-26 22:32:57 -08:00
}
2022-11-27 10:41:24 -08:00
// Render the game as seen in the examples
2022-11-26 22:32:57 -08:00
auto operator<<(std::ostream & out, Game const& game) -> std::ostream & {
for (std::size_t y = 0; y < game.grid_.rows.size(); ++y) {
out << game.grid_.rows[y];
bool first = true;
for (std::size_t x = 0; x < game.grid_.rows[y].size(); ++x) {
auto c = game.grid_.rows[y][x];
if (IsUnit(c)) {
if (first) {
first = false;
out << " ";
} else {
out << ", ";
}
2022-11-27 10:41:24 -08:00
Coord const coord {std::int64_t(x),std::int64_t(y)};
out << c << "(" << game.units_.at(coord) << ")";
2022-11-26 22:32:57 -08:00
}
}
out << std::endl;
}
return out;
}
// Determine the best target to attack from the current position, if there is one.
2022-11-27 10:41:24 -08:00
auto Game::PickTarget(Coord my_coord, char my_team) -> std::map<Coord, int>::iterator
2022-11-26 10:01:10 -08:00
{
2022-11-27 10:41:24 -08:00
std::map<Coord, int>::iterator best = units_.end();
2022-11-26 22:32:57 -08:00
for (auto const dir : reading_order) {
2022-11-27 10:41:24 -08:00
auto const here = dir + my_coord;
2022-11-26 22:32:57 -08:00
if (IsOpposed(my_team, grid_[here])) {
2022-11-27 10:41:24 -08:00
auto it = units_.find(here);
if (best == units_.end() || best->second > it->second) {
2022-11-26 22:32:57 -08:00
best = it;
}
2022-11-26 10:01:10 -08:00
}
}
return best;
}
2022-11-26 22:32:57 -08:00
// Determine the best location to move to, if there is one.
auto Game::Move(Coord my_coord, char my_team) -> std::optional<Coord>
2022-11-26 10:01:10 -08:00
{
2022-11-26 22:32:57 -08:00
std::optional<Coord> best_start;
2022-11-27 10:41:24 -08:00
Coord best_end; // initialized when best_start is non-empty
int best_distance; // initialized when best_start is non-empty
2022-11-26 22:32:57 -08:00
2022-11-27 11:01:31 -08:00
std::queue<std::tuple<Coord, Coord, int>> todo;
2022-11-26 22:32:57 -08:00
for (auto const dir : reading_order) {
2022-11-27 10:41:24 -08:00
auto const start = my_coord + dir;
2022-11-26 22:32:57 -08:00
if (grid_[start] == '.') {
2022-11-27 11:01:31 -08:00
todo.push({start, start, 1});
2022-11-26 22:32:57 -08:00
}
2022-11-26 10:01:10 -08:00
}
2022-11-27 10:41:24 -08:00
std::set<Coord> seen;
2022-11-26 10:01:10 -08:00
while (!todo.empty()) {
2022-11-27 10:41:24 -08:00
auto const [start, cursor, steps] = todo.front();
2022-11-27 11:01:31 -08:00
todo.pop();
2022-11-26 10:01:10 -08:00
2022-11-26 22:32:57 -08:00
if (best_start) {
if (best_distance < steps) break;
if (best_start && !IsBefore()(cursor, best_end)) continue;
} else {
if (!seen.insert(cursor).second) { continue; }
for (auto const dir : reading_order) {
auto const next = cursor + dir;
if (grid_[next] == '.') {
2022-11-27 11:01:31 -08:00
todo.push({start, cursor + dir, steps+1});
2022-11-26 22:32:57 -08:00
}
2022-11-26 10:01:10 -08:00
}
}
2022-11-27 10:41:24 -08:00
if (PickTarget(cursor, my_team) != units_.end()) {
2022-11-26 22:32:57 -08:00
best_start = start;
2022-11-26 10:01:10 -08:00
best_end = cursor;
best_distance = steps;
}
}
return best_start;
}
2022-11-26 22:32:57 -08:00
auto Game::Simulate() -> std::optional<int> {
2022-11-26 10:01:10 -08:00
for (int rounds = 0;; rounds++) {
2022-11-26 22:32:57 -08:00
if (debug_out_) {
*debug_out_ << "After " << rounds << " rounds:" << std::endl
<< *this << std::endl;
2022-11-26 10:01:10 -08:00
}
// determine order of unit updates
2022-11-26 22:32:57 -08:00
std::set<Coord, IsBefore> turn_order;
2022-11-27 10:41:24 -08:00
for (auto const& [k,_] : units_) {
2022-11-26 22:32:57 -08:00
turn_order.insert(k);
}
2022-11-26 10:01:10 -08:00
2022-11-26 22:32:57 -08:00
for (auto active_coord : turn_order) {
2022-11-27 10:41:24 -08:00
auto const active_team = grid_[active_coord];
2022-11-26 10:01:10 -08:00
// Detect when no targets remain for this unit and end the game
2022-11-27 10:41:24 -08:00
auto game_over = true;
2024-09-05 20:10:34 -07:00
for (auto [k, v] : grid_) {
2022-11-26 10:01:10 -08:00
if (IsOpposed(active_team, v)) game_over = false;
2024-09-05 20:10:34 -07:00
}
2022-11-26 10:01:10 -08:00
if (game_over) {
int total_hp = 0;
2022-11-27 10:41:24 -08:00
for (auto const& [_,v] : units_) { total_hp += v; }
2022-11-26 22:32:57 -08:00
return {rounds * total_hp};
2022-11-26 10:01:10 -08:00
}
// Try to pick a target without moving
2022-11-26 22:32:57 -08:00
auto target = PickTarget(active_coord, active_team);
2022-11-26 10:01:10 -08:00
// only move when necessary
2022-11-27 10:41:24 -08:00
if (target == units_.end()) {
2022-11-26 10:01:10 -08:00
// move if we can
2022-11-26 22:32:57 -08:00
if (auto const destination = Move(active_coord, active_team)) {
std::swap(grid_[*destination], grid_[active_coord]);
2022-11-26 10:01:10 -08:00
2022-11-27 10:41:24 -08:00
auto const it = units_.find(active_coord);
units_[*destination] = it->second;
units_.erase(it);
2022-11-26 10:01:10 -08:00
active_coord = *destination;
}
2022-11-26 22:32:57 -08:00
target = PickTarget(active_coord, active_team);
2022-11-26 10:01:10 -08:00
}
// target in range, do the attack
2022-11-27 10:41:24 -08:00
if (target != units_.end()) {
auto & [target_coord, target_hp] = *target;
2022-11-26 22:32:57 -08:00
auto damage = active_team == 'G' ? g_damage_ : e_damage_;
// lethal
if (target_hp <= damage) {
if (no_elves_can_die && grid_[target_coord] == 'E') return {};
grid_[target_coord] = '.';
turn_order.erase(target_coord);
2022-11-27 10:41:24 -08:00
units_.erase(target); // invalidates target, target_coord, target_hp
2022-11-26 22:32:57 -08:00
} else {
target_hp -= damage;
2022-11-26 10:01:10 -08:00
}
}
}
}
}
2022-11-26 22:32:57 -08:00
auto Part2(Game game) {
game.no_elves_can_die = true;
int best;
auto const attempt = [&](int const power) {
auto g = game;
g.e_damage_ = power;
return g.Simulate();
};
int too_low = 3;
int high = 4;
for(;;) {
if (auto outcome = attempt(high)) {
best = *outcome; break;
}
too_low = high;
high *= 2;
}
while (too_low+1 < high) {
auto x = std::midpoint(too_low, high);
if (auto outcome = attempt(x)) {
best = *outcome; high = x;
} else {
too_low = x;
}
}
2022-11-26 10:01:10 -08:00
2022-11-26 22:32:57 -08:00
return std::make_pair(best, high);
}
2022-11-26 10:01:10 -08:00
} // namespace
TEST_SUITE("2018-15 examples") {
2022-11-26 22:32:57 -08:00
auto case1(int expect, std::string start, std::string end) -> void {
std::istringstream in { std::move(start) };
std::ostringstream out;
Game game {Grid::Parse(in)};
auto outcome = game.Simulate();
out << game;
CHECK(out.str() == end);
CHECK(outcome == std::optional<int>{expect});
}
TEST_CASE("example 1") {
case1(27730,
"#######\n"
"#.G...#\n"
"#...EG#\n"
"#.#.#G#\n"
"#..G#E#\n"
"#.....#\n"
"#######\n",
"#######\n"
"#G....# G(200)\n"
"#.G...# G(131)\n"
"#.#.#G# G(59)\n"
"#...#.#\n"
"#....G# G(200)\n"
"#######\n");
}
TEST_CASE("example 2") {
case1(36334,
"#######\n"
"#G..#E#\n"
"#E#E.E#\n"
"#G.##.#\n"
"#...#E#\n"
"#...E.#\n"
"#######\n",
"#######\n"
"#...#E# E(200)\n"
"#E#...# E(197)\n"
"#.E##.# E(185)\n"
"#E..#E# E(200), E(200)\n"
"#.....#\n"
"#######\n");
}
TEST_CASE("example 3") {
case1(39514,
"#######\n"
"#E..EG#\n"
"#.#G.E#\n"
"#E.##E#\n"
"#G..#.#\n"
"#..E#.#\n"
"#######\n",
"#######\n"
"#.E.E.# E(164), E(197)\n"
"#.#E..# E(200)\n"
"#E.##.# E(98)\n"
"#.E.#.# E(200)\n"
"#...#.#\n"
"#######\n");
}
TEST_CASE("example 4") {
case1(27755,
"#######\n"
"#E.G#.#\n"
"#.#G..#\n"
"#G.#.G#\n"
"#G..#.#\n"
"#...E.#\n"
"#######\n",
"#######\n"
"#G.G#.# G(200), G(98)\n"
"#.#G..# G(200)\n"
"#..#..#\n"
"#...#G# G(95)\n"
"#...G.# G(200)\n"
"#######\n");
}
TEST_CASE("example 5") {
case1(28944,
"#######\n"
"#.E...#\n"
"#.#..G#\n"
"#.###.#\n"
"#E#G#G#\n"
"#...#G#\n"
"#######\n",
"#######\n"
"#.....#\n"
"#.#G..# G(200)\n"
"#.###.#\n"
"#.#.#.#\n"
"#G.G#G# G(98), G(38), G(200)\n"
"#######\n");
}
TEST_CASE("example 6") {
case1(18740,
"#########\n"
"#G......#\n"
"#.E.#...#\n"
"#..##..G#\n"
"#...##..#\n"
"#...#...#\n"
"#.G...G.#\n"
"#.....G.#\n"
"#########\n",
"#########\n"
"#.G.....# G(137)\n"
"#G.G#...# G(200), G(200)\n"
"#.G##...# G(200)\n"
"#...##..#\n"
"#.G.#...# G(200)\n"
"#.......#\n"
"#.......#\n"
"#########\n"
);
}
auto case2(int expect, std::string start, std::string end) -> void {
std::istringstream in { std::move(start) };
std::ostringstream out;
Game game {Grid::Parse(in)};
auto [outcome, e_damage] = Part2(game);
game.e_damage_ = e_damage;
game.Simulate();
out << game;
CHECK(out.str() == end);
CHECK(outcome == expect);
}
TEST_CASE("example 7") {
case2(4988,
"#######\n"
"#.G...#\n"
"#...EG#\n"
"#.#.#G#\n"
"#..G#E#\n"
"#.....#\n"
"#######\n",
"#######\n"
"#..E..# E(158)\n"
"#...E.# E(14)\n"
"#.#.#.#\n"
"#...#.#\n"
"#.....#\n"
"#######\n"
);
}
TEST_CASE("example 8") {
case2(31284,
"#######\n"
"#E..EG#\n"
"#.#G.E#\n"
"#E.##E#\n"
"#G..#.#\n"
"#..E#.#\n"
"#######\n",
"#######\n"
"#.E.E.# E(200), E(23)\n"
"#.#E..# E(200)\n"
"#E.##E# E(125), E(200)\n"
"#.E.#.# E(200)\n"
"#...#.#\n"
"#######\n");
}
TEST_CASE("example 9") {
case2(3478,
"#######\n"
"#E.G#.#\n"
"#.#G..#\n"
"#G.#.G#\n"
"#G..#.#\n"
"#...E.#\n"
"#######\n",
"#######\n"
"#.E.#.# E(8)\n"
"#.#E..# E(86)\n"
"#..#..#\n"
"#...#.#\n"
"#.....#\n"
"#######\n");
}
TEST_CASE("example 10") {
case2(6474,
"#######\n"
"#.E...#\n"
"#.#..G#\n"
"#.###.#\n"
"#E#G#G#\n"
"#...#G#\n"
"#######\n",
"#######\n"
"#...E.# E(14)\n"
"#.#..E# E(152)\n"
"#.###.#\n"
"#.#.#.#\n"
"#...#.#\n"
"#######\n");
}
TEST_CASE("example 11") {
case2(1140,
"#########\n"
"#G......#\n"
"#.E.#...#\n"
"#..##..G#\n"
"#...##..#\n"
"#...#...#\n"
"#.G...G.#\n"
"#.....G.#\n"
"#########\n",
"#########\n"
"#.......#\n"
"#.E.#...# E(38)\n"
"#..##...#\n"
"#...##..#\n"
"#...#...#\n"
"#.......#\n"
"#.......#\n"
"#########\n");
2022-11-26 10:01:10 -08:00
}
}
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-31 08:58:42 -08:00
{
auto const grid {Grid::Parse(in)};
2022-11-26 22:32:57 -08:00
Game game {std::move(grid)};
2023-01-31 09:15:15 -08:00
//game.debug_out_ = &out;
out << "Part 1: " << *Game{game}.Simulate() << std::endl;
out << "Part 2: " << Part2(std::move(game)).first << std::endl;
2022-11-26 10:01:10 -08:00
}