-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path02.cpp
89 lines (79 loc) · 2.02 KB
/
02.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
#include <chrono>
#include <iostream>
struct pos {
int row;
int col;
};
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
char kp1[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
char kp2[5][5] = {
{0, 0, '1', 0, 0}, //
{0, '2', '3', '4', 0}, //
{'5', '6', '7', '8', '9'}, //
{0, 'A', 'B', 'C', 0}, //
{0, 0, 'D', 0, 0}, //
};
std::string input;
int index = 0;
struct pos pt1 = {1, 1};
struct pos pt2 = {2, 0};
char code_pt1[100];
char code_pt2[100];
while (std::getline(std::cin, input)) {
for (char c : input) {
switch (c) {
case 'U':
if (pt1.row > 0) {
pt1.row -= 1;
}
if (pt2.row > 0 && kp2[pt2.row - 1][pt2.col] != 0) {
pt2.row -= 1;
}
break;
case 'R':
if (pt1.col < 2) {
pt1.col += 1;
}
if (pt2.col < 4 && kp2[pt2.row][pt2.col + 1] != 0) {
pt2.col += 1;
}
break;
case 'D':
if (pt1.row < 2) {
pt1.row += 1;
}
if (pt2.row < 4 && kp2[pt2.row + 1][pt2.col] != 0) {
pt2.row += 1;
}
break;
case 'L':
if (pt1.col > 0) {
pt1.col -= 1;
}
if (pt2.col > 0 && kp2[pt2.row][pt2.col - 1] != 0) {
pt2.col -= 1;
}
break;
}
}
code_pt1[index] = kp1[pt1.row][pt1.col];
code_pt2[index] = kp2[pt2.row][pt2.col];
index++;
}
code_pt1[index] = 0;
code_pt2[index] = 0;
std::cout << "--- Day 2: Bathroom Security ---\n";
std::cout << "Part 1: " << code_pt1 << "\n";
std::cout << "Part 2: " << code_pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << duration.count() << " μs"
<< "\n";
return EXIT_SUCCESS;
}