-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_03b.cpp
67 lines (61 loc) · 1.67 KB
/
day_03b.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
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
struct Point {
int row;
int col;
bool operator == (const Point& p) const {
return p.row ==row && p.col == col;
}
};
struct PointHasher {
std::size_t operator () (const Point& p) const {
return (p.row << 10) + p.col;
}
};
void updatePoint(Point& current, const char dir) {
if (dir == '<') {
current.col--;
} else if (dir == '>') {
current.col++;
} else if (dir == '^') {
current.row--;
} else if (dir == 'v') {
current.row++;
} else {
std::cout << "This should not happen";
std::cout << "Unknown symbol: " << dir << '\n';
exit(0);
}
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_03_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::ifstream file(input);
while (std::getline(file, line)) { // to allow for sample input
std::unordered_set<Point, PointHasher> seen;
std::array<Point, 2> currents = {{Point(), Point()}};
currents[0].row = 0;
currents[0].col = 0;
currents[1].row = 0;
currents[1].col = 0;
seen.insert(currents[0]);
bool original_santa = true;
for(const auto ele : line) {
if (original_santa) {
updatePoint(currents[0], ele);
seen.insert(currents[0]);
} else {
updatePoint(currents[1], ele);
seen.insert(currents[1]);
}
original_santa = !original_santa;
}
std::cout << seen.size() << '\n';
}
return 0;
}