-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_01b.cpp
86 lines (80 loc) · 2.12 KB
/
day_01b.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
#include <fstream>
#include <iostream>
#include <unordered_set>
#include <string>
struct Point {
int x;
int y;
bool operator == (const Point& p) const {
return p.x == x && p.y == y;
}
};
struct hasher {
std::size_t operator () (const Point& p) const {
return p.x * 10000 + p.y;
}
};
bool move (const std::size_t& start, const std::size_t& end, Point& p, int& direction, const std::string& instructions, std::unordered_set<Point, hasher>& visited) {
if (instructions[start] == 'L') {
direction--;
} else {
direction++;
}
if(direction < 0) {
direction += 4;
}
direction %= 4;
// Instead of using substr and then stoi can use this:
int num = 0;
for (std::size_t idx = start+1; idx < end; idx++) {
num = num * 10 + (instructions[idx] - '0');
}
for(int i = 0; i < num; i++) {
if (direction == 0) {
p.y+=1;
}
else if (direction == 1) {
p.x+=1;
}
else if (direction == 2) {
p.y-=1;
}
else {
p.x-=1;
}
const auto [it, inserted] = visited.insert(p);
if (!inserted) {
std::cout << std::abs(p.x) + std::abs(p.y) << '\n';
return true;
}
}
return false;
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_01_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string instructions;
std::getline(file, instructions);
Point p;
p.x = 0;
p.y = 0;
int direction = 0;
std::unordered_set<Point, hasher> visited;
visited.insert(p);
std::size_t start = 0;
const std::string delimiter = ", ";
std::size_t end = instructions.find(delimiter, start);
while(end != std::string::npos) {
if(move(start, end, p, direction, instructions, visited)) {
return 0;
}
start = end + delimiter.size();
end = instructions.find(delimiter, start);
}
move(start, instructions.size(), p, direction, instructions, visited);
// Question guarantees that there will be at least 1 point revisited.
return 0;
}