-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path19.cpp
85 lines (71 loc) · 1.9 KB
/
19.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
#include <algorithm>
#include <chrono>
#include <iostream>
#include <vector>
using std::string;
using std::vector;
struct Point {
int row;
int col;
};
Point directions[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
enum direction {
NORTH,
EAST,
SOUTH,
WEST
};
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int w = 0;
vector<vector<char>> grid;
std::string line;
while (std::getline(std::cin, line)) {
vector<char> row(line.begin(), line.end());
grid.push_back(row);
w = std::max(w, static_cast<int>(row.size()));
}
const int h = static_cast<int>(grid.size());
// find | on first row
unsigned int d = SOUTH;
string letters;
int r = 0;
int c = static_cast<int>(std::find(grid[0].begin(), grid[0].end(), '|') -
grid[0].begin());
int steps = 0;
while (true) {
steps += 1;
r = r + directions[d].row;
c = c + directions[d].col;
if (r >= h || c >= w) {
break;
}
const char tile = grid[r][c];
if (isalpha(tile)) {
letters.push_back(tile);
} else if (tile == '+') {
d = (d == NORTH || d == SOUTH) ? EAST : NORTH;
const int nr = r + directions[d].row;
const int nc = c + directions[d].col;
if (nr < h && nc < w) {
const char tile = grid[nr][nc];
if (isalpha(tile) || tile == '-' || tile == '|') {
continue;
}
}
d += 2;
} else if (tile != '-' && tile != '|') {
break;
}
}
std::cout << "--- Day 19: A Series of Tubes ---\n";
std::cout << "Part 1: " << letters << "\n";
std::cout << "Part 2: " << steps << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}