-
Notifications
You must be signed in to change notification settings - Fork 2
/
08.cpp
103 lines (92 loc) · 2.26 KB
/
08.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
95
96
97
98
99
100
101
102
103
#include <chrono>
#include <iostream>
void shift_row(char grid[6][50], int r, int n) {
char copy[50];
for (unsigned long c = 0; c < 50; c++) {
copy[c] = grid[r][c];
}
for (int c = 0; c < 50; c++) {
int src = c - n;
if (src < 0) {
src += 50;
}
grid[r][c] = copy[src];
}
}
void shift_column(char grid[6][50], int c, int n) {
char copy[6];
for (int r = 0; r < 6; r++) {
copy[r] = grid[r][c];
}
for (int r = 0; r < 6; r++) {
int src = r - n;
if (src < 0) {
src += 6;
}
grid[r][c] = copy[src];
}
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int pt1 = 0;
std::string line;
char grid[6][50] = {{0}};
for (unsigned int r = 0; r < 6; r++) {
for (unsigned int c = 0; c < 50; c++) {
grid[r][c] = 0;
}
}
while (std::getline(std::cin, line)) {
if (line.compare(0, 4, "rect") == 0) {
unsigned int i = 5;
int width = std::stoi(&line[i]);
while (line[i - 1] != 'x') {
i++;
}
int height = std::stoi(&line[i]);
for (int r = 0; r < height; r++) {
for (int c = 0; c < width; c++) {
grid[r][c] = 1;
}
}
} else {
char orientation = line[7];
unsigned int i = 7;
while (line[i] < '0' || line[i] > '9') {
i++;
}
int src = std::stoi(&line[i]);
i += 4;
while (line[i] < '0' || line[i] > '9') {
i++;
}
int amount = std::stoi(&line[i]);
if (orientation == 'c') {
shift_column(grid, src, amount);
} else {
shift_row(grid, src, amount);
}
}
}
for (unsigned int r = 0; r < 6; r++) {
for (unsigned int c = 0; c < 50; c++) {
pt1 += grid[r][c];
}
}
std::cout << "--- Day 8: Two-Factor Authentication ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2:\n";
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 50; c++) {
pt1 += grid[r][c];
std::cout << (grid[r][c] ? '#' : ' ');
}
std::cout << "\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;
}