parent
c092f805fe
commit
8178a8e7a3
2 changed files with 93 additions and 3 deletions
@ -1,9 +1,97 @@ |
||||
#include "Day08.h" |
||||
|
||||
Result Day08::Task1() { |
||||
return Day::Task1(); |
||||
size_t w, h; |
||||
vector<vector<int>> grid = parseGrid(w, h); |
||||
|
||||
size_t count = w * 2 + (h - 2) * 2; |
||||
|
||||
auto checkVisible = [grid, w, h](size_t rIdx, size_t cIdx) -> bool { |
||||
auto checkDir = [grid, w, h, rIdx, cIdx](int rV, int cV){ |
||||
set<int> values; |
||||
if (rV != 0) |
||||
for (int rLook = int(rIdx) + rV; rLook >= 0 && rLook < h; rLook += rV) |
||||
values.insert(grid[rLook][cIdx]); |
||||
if (cV != 0) |
||||
for (int cLook = int(cIdx) + cV; cLook >= 0 && cLook < w; cLook += cV) |
||||
values.insert(grid[rIdx][cLook]); |
||||
return *std::max_element(values.begin(), values.end()) < grid[rIdx][cIdx]; |
||||
}; |
||||
|
||||
return checkDir(1, 0) || |
||||
checkDir(-1, 0) || |
||||
checkDir(0, 1) || |
||||
checkDir(0, -1); |
||||
}; |
||||
|
||||
for (size_t rIdx = 1; rIdx < h - 1; rIdx++) |
||||
for (size_t cIdx = 1; cIdx < w - 1; cIdx++) |
||||
if (checkVisible(rIdx, cIdx)) count++; |
||||
|
||||
|
||||
return to_string(count); |
||||
} |
||||
|
||||
Result Day08::Task2() { |
||||
return Day::Task2(); |
||||
size_t w, h; |
||||
vector<vector<int>> grid = parseGrid(w, h); |
||||
|
||||
auto look = [grid, w, h](size_t rIdx, size_t cIdx, int rV, int cV){ |
||||
int dist = 0; |
||||
if (cV != 0){ |
||||
for (int cLook = int(cIdx) + cV; 0 <= cLook && cLook < w; cLook += cV){ |
||||
dist++; |
||||
if (grid[rIdx][cLook] >= grid[rIdx][cIdx]) |
||||
break; |
||||
} |
||||
} |
||||
if (rV != 0){ |
||||
for (int rLook = int(rIdx) + rV; 0 <= rLook && rLook < h; rLook += rV){ |
||||
dist++; |
||||
if (grid[rLook][cIdx] >= grid[rIdx][cIdx]) |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return dist; |
||||
}; |
||||
|
||||
auto lookVertical = [look](size_t rIdx, size_t cIdx, int dir) -> int { |
||||
return look(rIdx, cIdx, dir, 0); |
||||
}; |
||||
|
||||
auto lookHorizontal = [look](size_t rIdx, size_t cIdx, int dir) -> int { |
||||
return look(rIdx, cIdx, 0, dir); |
||||
}; |
||||
|
||||
set<int> scores; |
||||
for (size_t rIdx = 1; rIdx < h - 1; rIdx++){ |
||||
for (size_t cIdx = 1; cIdx < w - 1; cIdx++){ |
||||
int up = lookVertical(rIdx, cIdx, -1); |
||||
int down = lookVertical(rIdx, cIdx, 1); |
||||
int left = lookHorizontal(rIdx, cIdx, -1); |
||||
int right = lookHorizontal(rIdx, cIdx, 1); |
||||
scores.insert(up * down * left * right); |
||||
} |
||||
} |
||||
|
||||
int max = *std::max_element(scores.begin(), scores.end()); |
||||
|
||||
return to_string(max); |
||||
} |
||||
|
||||
vector<vector<int>> Day08::parseGrid(size_t &w, size_t &h) { |
||||
vector<vector<int>> grid; |
||||
|
||||
for (const string &line : input){ |
||||
vector<int> row; |
||||
for (const char &item: line) |
||||
row.emplace_back(item - '0'); |
||||
grid.push_back(row); |
||||
} |
||||
|
||||
w = input[0].size(); |
||||
h = input.size(); |
||||
|
||||
return grid; |
||||
} |
Loading…
Reference in new issue