|
|
|
@ -5,21 +5,21 @@ Result Day03::Task1() { |
|
|
|
|
|
|
|
|
|
auto isSymbol = [this](int32_t x, int32_t y) -> bool { |
|
|
|
|
if (x < 0 || x > input[0].length() - 1 || |
|
|
|
|
y < 0 || y > input.size() - 1){ |
|
|
|
|
y < 0 || y > input.size() - 1) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
char c = input[y][x]; |
|
|
|
|
return !isDigit(c) && c != '.'; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
for (const auto &[number, position] : parseEngineNumbers()){ |
|
|
|
|
for (const auto &[number, position]: parseEngineNumbers()) { |
|
|
|
|
auto &[value, size] = number; |
|
|
|
|
auto &[x, y] = position; |
|
|
|
|
|
|
|
|
|
bool symbolFound = false; |
|
|
|
|
for (int32_t sx = x - 1; sx < x + size + 1; sx++){ |
|
|
|
|
for (int32_t sy = y - 1; sy < y + 2; sy++){ |
|
|
|
|
if (isSymbol(sx, sy)){ |
|
|
|
|
for (int32_t sx = x - 1; sx < x + size + 1; sx++) { |
|
|
|
|
for (int32_t sy = y - 1; sy < y + 2; sy++) { |
|
|
|
|
if (isSymbol(sx, sy)) { |
|
|
|
|
symbolFound = true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
@ -39,20 +39,20 @@ Result Day03::Task2() { |
|
|
|
|
|
|
|
|
|
auto isStar = [this](int32_t x, int32_t y) -> bool { |
|
|
|
|
if (x < 0 || x > input[0].length() - 1 || |
|
|
|
|
y < 0 || y > input.size() - 1){ |
|
|
|
|
y < 0 || y > input.size() - 1) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return input[y][x] == '*'; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
for (const auto &[number, position] : parseEngineNumbers()){ |
|
|
|
|
for (const auto &[number, position]: parseEngineNumbers()) { |
|
|
|
|
auto &[value, size] = number; |
|
|
|
|
auto &[x, y] = position; |
|
|
|
|
|
|
|
|
|
for (int32_t sx = x - 1; sx < x + size + 1; sx++){ |
|
|
|
|
for (int32_t sy = y - 1; sy < y + 2; sy++){ |
|
|
|
|
if (isStar(sx, sy)){ |
|
|
|
|
if (stars.contains({sx, sy})){ |
|
|
|
|
for (int32_t sx = x - 1; sx < x + size + 1; sx++) { |
|
|
|
|
for (int32_t sy = y - 1; sy < y + 2; sy++) { |
|
|
|
|
if (isStar(sx, sy)) { |
|
|
|
|
if (stars.contains({sx, sy})) { |
|
|
|
|
stars[{sx, sy}].insert({number, position}); |
|
|
|
|
} else { |
|
|
|
|
stars[{sx, sy}] = {{number, position}}; |
|
|
|
@ -62,8 +62,8 @@ Result Day03::Task2() { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (auto &[position, numbers] : stars){ |
|
|
|
|
if (numbers.size() == 2){ |
|
|
|
|
for (auto &[position, numbers]: stars) { |
|
|
|
|
if (numbers.size() == 2) { |
|
|
|
|
sum += numbers.begin()->first.first * (++numbers.begin())->first.first; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -74,23 +74,25 @@ Result Day03::Task2() { |
|
|
|
|
vector<Day03::EngineNumber> Day03::parseEngineNumbers() const { |
|
|
|
|
vector<EngineNumber> numbers; |
|
|
|
|
|
|
|
|
|
for (size_t y = 0; y < input.size(); y++){ |
|
|
|
|
const string& line = input[y]; |
|
|
|
|
for (size_t y = 0; y < input.size(); y++) { |
|
|
|
|
const string &line = input[y]; |
|
|
|
|
uint32_t value = 0; |
|
|
|
|
uint8_t valueLength = 0; |
|
|
|
|
bool readingValue = false; |
|
|
|
|
|
|
|
|
|
for (size_t x = 0; x < line.length(); x++){ |
|
|
|
|
for (size_t x = 0; x < line.length(); x++) { |
|
|
|
|
uint8_t digit; |
|
|
|
|
if (isDigit(line[x], digit)){ |
|
|
|
|
if (isDigit(line[x], digit)) { |
|
|
|
|
value = value * 10 + digit; |
|
|
|
|
valueLength++; |
|
|
|
|
readingValue = true; |
|
|
|
|
if (x == line.length() - 1) |
|
|
|
|
numbers.push_back({{value, valueLength}, {x - valueLength, y}}); |
|
|
|
|
numbers.push_back({{value, valueLength}, |
|
|
|
|
{x - valueLength, y}}); |
|
|
|
|
} else { |
|
|
|
|
if (readingValue) |
|
|
|
|
numbers.push_back({{value, valueLength}, {x - valueLength, y}}); |
|
|
|
|
numbers.push_back({{value, valueLength}, |
|
|
|
|
{x - valueLength, y}}); |
|
|
|
|
readingValue = false; |
|
|
|
|
valueLength = 0; |
|
|
|
|
value = 0; |
|
|
|
|