-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.cpp
109 lines (96 loc) · 2.77 KB
/
3.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <vector>
#include <memory>
#include <unordered_map>
const std::vector<std::pair<int, int>> moves{
{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}
};
template<class T>
void print_matrix(const std::vector<T>& matrix) {
for (const auto& row : matrix) {
for (const auto& e : row) std::cout << e;
std::cout << std::endl;
}
}
int part1(const std::vector<std::string>& G) {
int m = G.size();
int n = G[0].size();
std::vector<std::vector<bool>> adj(m, std::vector<bool>(n, false));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (G[i][j] == '.' || std::isdigit(G[i][j])) continue;
for (const auto& [di, dj] : moves) {
int p = i + di;
int q = j + dj;
if (p < 0 || p >= m || q < 0 || q >= n) continue;
adj[p][q] = true;
}
}
}
int cur = 0;
int ret = 0;
for (int i = 0; i < m; ++i) {
bool is_adj = false;
for (int j = 0; j <= n; ++j) {
if (j == n || !std::isdigit(G[i][j])) {
if (is_adj) ret += cur;
cur = 0;
is_adj = false;
} else {
is_adj |= adj[i][j];
cur = cur * 10 + (G[i][j] - '0');
}
}
}
return ret;
}
int part2(const std::vector<std::string>& G) {
int m = G.size();
int n = G[0].size();
std::unordered_map<int, std::vector<int>> map;
int cur = 0;
for (int i = 0; i < m; ++i) {
std::vector<int> adj;
bool found = false;
for (int j = 0; j <= n; ++j) {
if (j == n || !std::isdigit(G[i][j])) {
for (const int id : adj) {
map[id].push_back(cur);
}
cur = 0;
adj.clear();
found = false;
} else {
cur = cur * 10 + (G[i][j] - '0');
for (const auto& [di, dj] : moves) {
int p = i + di;
int q = j + dj;
if (p < 0 || p >= m || q < 0 || q >= n) continue;
if (!found && G[p][q] == '*') {
adj.push_back(p * n + q);
found = true;
}
}
}
}
}
int ret = 0;
for (const auto& [k, v] : map) {
if (v.size() == 2) {
ret += v[0] * v[1];
}
}
return ret;
}
int main() {
std::ifstream f("3.txt");
std::vector<std::string> G;
std::string line;
std::uint64_t ans = 0;
while (std::getline(f, line)) {
G.push_back(std::move(line));
}
std::cout << part2(G) << std::endl;
}