-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.cc
81 lines (72 loc) · 2.23 KB
/
6.cc
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
#include <iostream>
#include <map>
static bool hasPrefix(std::string string, std::string prefix) {
return std::mismatch(prefix.begin(), prefix.end(), string.begin()).first == prefix.end();
}
int main() {
bool grid[1000][1000];
int brightness[1000][1000];
for (int i = 0; i < 1000; ++i) {
for (int j = 0; j < 1000; ++j) {
grid[i][j] = false;
brightness[i][j] = 0;
}
}
std::string line;
while (std::getline(std::cin, line)) {
enum class Command {
On,
Off,
Toggle,
} command;
static std::map<std::string, Command> prefixes {
{ "turn on", Command::On },
{ "turn off", Command::Off },
{ "toggle", Command::Toggle },
};
int x1;
int y1;
int x2;
int y2;
for (auto pair : prefixes) {
if (hasPrefix(line, pair.first)) {
command = pair.second;
sscanf(line.c_str() + pair.first.length() + 1, "%d,%d through %d,%d", &x1, &y1, &x2, &y2);
break;
}
}
for (int i = std::min(x1, x2); i <= std::max(x1, x2); ++i) {
for (int j = std::min(y1, y2); j <= std::max(y1, y2); ++j) {
switch (command) {
case Command::On:
grid[i][j] = true;
++brightness[i][j];
break;
case Command::Off:
grid[i][j] = false;
if (brightness[i][j]) {
--brightness[i][j];
}
break;
case Command::Toggle:
grid[i][j] = !grid[i][j];
brightness[i][j] += 2;
break;
}
}
}
}
int count = 0;
int totalBrightness = 0;
for (int i = 0; i < 1000; ++i) {
for (int j = 0; j < 1000; ++j) {
if (grid[i][j]) {
++count;
}
totalBrightness += brightness[i][j];
}
}
std::cout << count << std::endl;
std::cout << totalBrightness << std::endl;
return 0;
}