Initial commit

This commit is contained in:
2025-05-21 14:26:13 +03:00
commit b33e3df329
11 changed files with 439 additions and 0 deletions

74
838/838-1.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include <string>
#include <iostream>
#include <set>
namespace {
enum Dominoes : char {
Dot = '.',
L = 'L',
R = 'R',
};
class Solution {
public:
std::string pushDominoes(std::string dominoes) {
bool has_actions = true;
while (has_actions) {
size_t processed_dot = -1;
size_t actions_found = 0;
for (size_t i = 0; i < dominoes.size(); ++i) {
char d = dominoes[i];
if ((d == L || d == R) && processed_dot != i) {
++actions_found;
bool left_dot = false;
bool right_dot = false;
if (i > 0 && (dominoes[i - 1] == Dot && processed_dot != (i - 1)))
left_dot = true;
if (i < (dominoes.size() - 1) && dominoes[i + 1] == Dot)
right_dot = true;
if (d == L && left_dot) {
if (i > 1 && dominoes[i - 2] == R && processed_dot != (i - 2))
--actions_found;
else {
dominoes[i - 1] = L;
processed_dot = (i - 1);
}
}
else if (d == R && right_dot) {
if (i < (dominoes.size() - 2) && dominoes[i + 2] == L)
--actions_found;
else {
dominoes[i + 1] = R;
processed_dot = (i + 1);
}
}
else
--actions_found;
}
}
if (!actions_found)
has_actions = false;
std::cout << "step " << dominoes << '\n';
}
return dominoes;
}
};
}
#include <gtest/gtest.h>
TEST(P838_1, 1) {
std::string input = ".L.R...LR..L..";
Solution s;
auto result = s.pushDominoes(input);
EXPECT_EQ(result, std::string("LL.RR.LLRRLL.."));
}

90
838/838-2.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include <string>
#include <iostream>
#include <vector>
#include <span>
namespace {
enum Dominoes : char {
Dot = '.',
L = 'L',
R = 'R',
};
using Range = std::tuple<size_t, size_t>;
using Ranges = std::vector<Range>;
class Solution {
Ranges findRanges(const std::string& dominoes) {
Ranges ranges;
ranges.reserve(dominoes.size() / 2);
size_t start = -1;
for (size_t i = 0; i < dominoes.size(); ++i) {
char d = dominoes[i];
if (d == Dot && start == -1) {
start = i;
} else if (d != Dot && start != -1) {
ranges.emplace_back(start, i);
start = -1;
}
}
if (start != -1) {
ranges.emplace_back(start, dominoes.size());
}
return ranges;
}
void processRange(const Range& range, std::string& dominoes) {
bool left_r = false;
bool right_l = false;
const auto& [start, end] = range;
if (start > 0 && dominoes[start - 1] == R)
left_r = true;
if (end < (dominoes.size()) && dominoes[end] == L)
right_l = true;
if (left_r && right_l) {
/*
* 3 4 5 6 7
* . . . . L
*/
auto difference = end - start;
auto steps = difference / 2;
for (auto i = 0; i < steps; ++i) {
dominoes[start + i] = R;
dominoes[end - 1 - i] = L;
}
} else if (left_r) {
for (auto i = start; i < end; ++i) dominoes[i] = R;
} else if (right_l) {
for (auto i = start; i < end; ++i) dominoes[i] = L;
}
}
public:
std::string pushDominoes(std::string dominoes) {
auto ranges = findRanges(dominoes);
for (const auto& range : ranges) {
auto& [start, end] = range;
std::cout << "[" << start << ", " << end << "]\n";
processRange(range, dominoes);
}
return dominoes;
}
};
}
#include <gtest/gtest.h>
TEST(P838_2, 1) {
std::string input = ".L.R...LR..L..";
Solution s;
auto result = s.pushDominoes(input);
EXPECT_EQ(result, std::string("LL.RR.LLRRLL.."));
}