-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc3.cpp
142 lines (119 loc) · 3.31 KB
/
aoc3.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
using namespace std;
vector<string> s;
int rows=140, cols=140;
long long part1() {
for(int i = 0; i < 140; ++i) {
string in;
cin >> in;
s.push_back(in);
}
long long sum = 0;
for(int i = 0; i < 140; ++i) {
int number = 0;
bool valid = 0;
for(int j = 0; j < 140; ++j) {
if(!isdigit(s[i][j])) {
if (valid) sum += number;
number = 0;
valid = 0;
}
else {
number = number * 10 + (s[i][j] - '0');
if (i > 0 && !isdigit(s[i-1][j]) && s[i-1][j] != '.') valid = 1;
else if (i < 139 && !isdigit(s[i+1][j]) && s[i+1][j] != '.') valid = 1;
else if (j > 0 && !isdigit(s[i][j-1]) && s[i][j-1] != '.') valid = 1;
else if (j < 139 && !isdigit(s[i][j+1]) && s[i][j+1] != '.') valid = 1;
else if (i < 139 && j < 139 && !isdigit(s[i+1][j+1]) && s[i+1][j+1] != '.') valid = 1;
else if (i < 139 && j > 0 && !isdigit(s[i+1][j-1]) && s[i+1][j-1] != '.') valid = 1;
else if (i > 0 && j < 139 && !isdigit(s[i-1][j+1]) && s[i-1][j+1] != '.') valid = 1;
else if (i > 0 && j > 0 && !isdigit(s[i-1][j-1]) && s[i-1][j-1] != '.') valid = 1;
}
}
if(valid) {
sum += number;
}
}
return sum;
}
void parse() {
for(int i = 0; i < rows; ++i) {
string in;
cin >> in;
s.push_back(in);
}
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
if(s[i][j] != '*') {
if(!isdigit(s[i][j])) {
s[i][j] = '.';
}
}
else {
int adj = 0;
if (i < 139 && j > 0 && isdigit(s[i+1][j-1])) ++adj;
if (i < 139 && isdigit(s[i+1][j]) && (j == 0 || !isdigit(s[i+1][j-1]))) ++adj;
if (i < 139 && j < 139 && isdigit(s[i+1][j+1]) && !isdigit(s[i+1][j])) ++adj;
if (i > 0 && j > 0 && isdigit(s[i-1][j-1])) ++adj;
if (i > 0 && isdigit(s[i-1][j]) && (j == 0 || !isdigit(s[i-1][j-1]))) ++adj;
if (i > 0 && j < 139 && isdigit(s[i-1][j+1]) && !isdigit(s[i-1][j])) ++adj;
if (j > 0 && isdigit(s[i][j-1])) ++adj;
if (j < 139 && isdigit(s[i][j+1])) ++adj;
if(adj != 2) {
s[i][j] = '.';
}
}
}
}
}
unordered_map<int, long long> ump;
long long part2() {
parse();
long long sum = 0;
for(int i = 0; i < rows; ++i) {
long long number = 0;
unordered_set<long long> store;
for(int j = 0; j < cols; ++j) {
if(!isdigit(s[i][j])) {
if(number) {
for(const auto& a : store) {
sum += number * ump[a];
ump[a] += number;
}
number = 0;
store.clear();
}
}
else {
number = number * 10 + (s[i][j] - '0');
if (i < 139 && s[i+1][j] == '*') store.insert((i+1)*140+j);
if (j > 0 && s[i][j-1] == '*') store.insert(i*140+j-1);
if (i < 139 && j < 139 && s[i+1][j+1] == '*') store.insert((i+1)*140+j+1);
if (i < 139 && j > 0 && s[i+1][j-1] == '*') store.insert(i*140+140+j-1);
if (i > 0 && s[i-1][j] == '*') store.insert(i*140-140+j);
if (j < 139 && s[i][j+1] == '*') store.insert(i*140+j+1);
if (i > 0 && j < 139 && s[i-1][j+1] == '*') store.insert(i*140-140+j+1);
if (i > 0 && j > 0 && s[i-1][j-1] == '*') store.insert(i*140-140+j-1);
}
}
if(number) {
for(const auto& a: store) {
sum += number * ump[a];
ump[a] += number;
}
}
}
return sum;
}
int main() {
cout << part2();
return 0;
}