-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_24a.cpp
63 lines (61 loc) · 1.91 KB
/
day_24a.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
#include <algorithm>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
std::pair<int, int> ParseToGetCoordinates(const std::string& directions) {
std::size_t index = 0;
std::pair<int, int> coordinates{0, 0};
while (index < directions.size()) {
if (directions.substr(index, 1) == "e") {
coordinates.first += 2;
index += 1;
} else if (directions.substr(index, 1) == "w") {
coordinates.first -= 2;
index += 1;
} else if (directions.substr(index, 2) == "ne") {
coordinates.first += 1;
coordinates.second += 1;
index += 2;
} else if (directions.substr(index, 2) == "nw") {
coordinates.first -= 1;
coordinates.second += 1;
index += 2;
} else if (directions.substr(index, 2) == "se") {
coordinates.first += 1;
coordinates.second -= 1;
index += 2;
} else if (directions.substr(index, 2) == "sw") {
coordinates.first -= 1;
coordinates.second -= 1;
index += 2;
}
}
return coordinates;
}
int main() {
std::fstream file{"../input/day_24_input"};
std::string line;
std::map<std::pair<int, int>, bool> tiles;
while (std::getline(file, line)) {
line.erase(std::remove_if(std::begin(line), std::end(line),
[](const char c) { return !isprint(c); }),
std::end(line));
if (line == "") continue;
const auto coordinates = ParseToGetCoordinates(line);
if (auto it = tiles.find(coordinates); it != tiles.end()) {
tiles[coordinates] = !tiles[coordinates];
} else {
tiles.insert({coordinates, false});
}
}
auto count = std::count_if(
std::begin(tiles), std::end(tiles),
[](const auto tile_with_val) { return !tile_with_val.second; });
std::cout << count << '\n';
return count;
}