-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc21_5.cpp
119 lines (110 loc) · 2.38 KB
/
aoc21_5.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string fname = "input.txt";
// Day 5.1: consider
void readline(string line, int& x1, int& x2, int& y1, int& y2) {
int delimPos = line.find(',');
x1 = stoi(line.substr(0, delimPos));
line = line.substr(delimPos + 1);
delimPos = line.find(' ');
y1 = stoi(line.substr(0, delimPos));
line = line.substr(delimPos + 4);
delimPos = line.find(',');
x2 = stoi(line.substr(0, delimPos));
line = line.substr(delimPos + 1);
y2 = stoi(line);
}
void handleHori(int x, int y1, int y2, int& res, vector<vector<int>>& g) {
if (y1 > y2) {
int temp = y1;
y1 = y2;
y2 = temp;
}
for (int y = y1; y <= y2; y++) {
g[x][y]++;
if (g[x][y] == 2) {
res++;
}
}
}
void handleVert(int y, int x1, int x2, int& res, vector<vector<int>>& g) {
if (x1 > x2) {
int temp = x1;
x1 = x2;
x2 = temp;
}
for (int x = x1; x <= x2; x++) {
g[x][y]++;
if (g[x][y] == 2) {
res++;
}
}
}
void goUpRight(int x1, int y1, int y2, int& res, vector<vector<int>>& g) {
for (int x = x1; y1 <= y2; x++) {
g[x][y1++]++;
if (g[x][y1 - 1] == 2) {
res++;
}
}
}
void goDownRight(int x1, int y1, int y2, int& res, vector<vector<int>>& g) {
for (int x = x1; y1 >= y2; x++) {
g[x][y1--]++;
if (g[x][y1 + 1] == 2) {
res++;
}
}
}
int solve5a(string fname, vector<vector<int>>& g) {
ifstream in(fname);
if (in.fail())
return -1;
string line;
int x1, x2, y1, y2;
int res = 0;
// getline(in, line);
int counter = 0;
// readline(line, x1, x2, y1, y2);
// cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
while (getline(in, line)) {
counter++;
readline(line, x1, x2, y1, y2);
if (x1 == x2) {
handleHori(x1, y1, y2, res, g);
} else if (y1 == y2) {
handleVert(y1, x1, x2, res, g);
} else {
if (x1 < x2 && y1 < y2) {
goUpRight(x1, y1, y2, res, g);
} else if (x1 > x2 && y1 < y2) {
goDownRight(x2, y2, y1, res, g);
} else if (x1 < x2 && y1 > y2) {
goDownRight(x1, y1, y2, res, g);
} else {
goUpRight(x2, y2, y1, res, g);
}
}
}
in.close();
int ans = 0;
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if (g[i][j] > 1) {
ans++;
}
}
}
cout << counter << endl;
cout << ans << endl;
return res;
}
int main() {
vector<vector<int>> g(1000, vector<int>(1000, 0));
cout << solve5a(fname, g) << endl;
return 0;
}