-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp
More file actions
75 lines (71 loc) · 2.02 KB
/
3.cpp
File metadata and controls
75 lines (71 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "common.h"
namespace {
int64_t p1(const std::vector<std::string> &in) {
int64_t r{0};
for (int j{0}; j < static_cast<int>(in.size()); ++j) {
for (int i{0}; i < static_cast<int>(in[j].length()); ++i) {
int num{0}, s{i}, e;
while (i < static_cast<int>(in[j].length()) && isdigit(in[j][i])) {
num = (num * 10) + (in[j][i] - '0');
++i;
}
e = i;
if (e > s) {
bool use{false};
for (int m{std::max(j - 1, 0)};
m < std::min(j + 2, static_cast<int>(in.size())); ++m) {
for (int n{std::max(s - 1, 0)};
n < std::min(e + 1, static_cast<int>(in[j].length())); ++n) {
if (in[m][n] != '.' && !isdigit(in[m][n])) {
use = true;
}
}
}
if (use)
r += num;
}
}
}
return r;
}
int64_t p2(const std::vector<std::string> &in) {
std::unordered_map<int, std::pair<int, int>> gears{};
for (int j{0}; j < static_cast<int>(in.size()); ++j) {
for (int i{0}; i < static_cast<int>(in[j].length()); ++i) {
int num{0}, s{i}, e;
while (i < static_cast<int>(in[j].length()) && isdigit(in[j][i])) {
num = (num * 10) + (in[j][i] - '0');
++i;
}
e = i;
if (e > s) {
for (int m{std::max(j - 1, 0)};
m < std::min(j + 2, static_cast<int>(in.size())); ++m) {
for (int n{std::max(s - 1, 0)};
n < std::min(e + 1, static_cast<int>(in[j].length())); ++n) {
if (in[m][n] == '*') {
int k = m * in[0].length() + n;
if (gears.find(k) == gears.end()) {
gears.emplace(k, std::make_pair(0, 1));
}
++gears[k].first;
gears[k].second *= num;
}
}
}
}
}
}
int64_t r{0};
for (const auto &[k, v] : gears) {
if (v.first == 2)
r += v.second;
}
return r;
}
} // namespace
int main() {
const auto &in = gb::readIn();
gb::writeOut(p1(in));
gb::writeOut(p2(in));
}