-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_05b.cpp
94 lines (87 loc) · 2.45 KB
/
day_05b.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
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
struct Point {
int row;
int col;
Point(const int row, const int col) : row(row), col(col) {}
Point operator + (const Point& p) const {
return Point(row + p.row, col + p.col);
}
bool operator != (const Point& p) const {
return row != p.row || col != p.col;
}
bool operator == (const Point& p) const {
return row == p.row && col == p.col;
}
friend std::ostream& operator << (std::ostream& os, const Point& p);
};
std::ostream& operator << (std::ostream& os, const Point& p) {
os << "(" << p.row << ", " << p.col << ")";
return os;
}
struct Point_hash {
std::size_t operator() (const Point& p) const {
return p.row ^ p.col;
}
};
std::tuple<Point, Point> convert_to_nums (const std::string& line) {
std::size_t start = 0;
const std::string delimiters = " ,";
std::size_t end = line.find_first_of(delimiters);
std::vector<int> vals;
int count = 0;
while (end != std::string::npos) {
if (count !=2) { // Account for ->
vals.push_back(std::stoi(line.substr(start, end - start)));
}
count++;
start = end + 1;
end = line.find_first_of(delimiters, start);
}
vals.push_back(std::stoi(line.substr(start, end - start)));
return {Point(vals[0], vals[1]), Point(vals[2], vals[3])};
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_05_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::unordered_map<Point, int, Point_hash> vents;
while(std::getline(file, line)) {
const auto [start, end] = convert_to_nums(line);
auto delta = Point(0, 0);
if (start.row == end.row) {
delta.col = std::abs(start.col - end.col) / (start.col - end.col);
} else if (start.col == end.col) {
delta.row = std::abs(start.row - end.row) / (start.row - end.row);
} else {
delta.col = std::abs(start.col - end.col) / (start.col - end.col);
delta.row = std::abs(start.row - end.row) / (start.row - end.row);
}
auto temp = end;
while (temp != start) {
if (vents.find(temp) == vents.end()) {
vents[temp] = 0;
}
vents[temp]++;
temp = temp + delta;
}
if (vents.find(temp) == vents.end()) {
vents[temp] = 0;
}
vents[temp]++;
}
int n = 0;
for (const auto& [p, count] : vents) {
if (count > 1) {
n++;
}
}
std::cout << n << '\n';
return 0;
}