-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_24b.cpp
116 lines (109 loc) · 3.52 KB
/
day_24b.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
104
105
106
107
108
109
110
111
112
113
114
115
116
#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;
}
const std::vector<std::pair<int, int>> additions{{2, 0}, {-2, 0}, {1, 1},
{1, -1}, {-1, 1}, {-1, -1}};
void AddRequredTiles(std::map<std::pair<int, int>, bool>& tiles) {
std::set<std::pair<int, int>> to_add;
for (auto& [coordinates, white_up] : tiles) {
for (const auto& addition : additions) {
const std::pair<int, int> new_coordinates{
coordinates.first + addition.first,
coordinates.second + addition.second};
if (tiles.find(new_coordinates) == tiles.end()) {
to_add.insert(new_coordinates);
}
}
}
for (const auto& coordinates : to_add) {
tiles[coordinates] = true;
}
}
void MakeArtLive(std::map<std::pair<int, int>, bool>& tiles) {
std::map<std::pair<int, int>, bool> to_flip;
for (auto& [coordinates, white_up] : tiles) {
int count = 0;
for (const auto& addition : additions) {
const std::pair<int, int> new_coordinates{
coordinates.first + addition.first,
coordinates.second + addition.second};
if (auto it = tiles.find(new_coordinates); it != tiles.end()) {
if (it->second == false) {
count++;
}
}
}
if (!white_up && (count == 0 || count > 2)) {
to_flip[coordinates] = true;
} else if (white_up && count == 2) {
to_flip[coordinates] = true;
}
}
for (auto& [coordinates, white_up] : tiles) {
if (to_flip[coordinates]) {
white_up = !white_up;
}
}
}
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});
}
}
for (int i = 0; i < 100; i++) {
AddRequredTiles(tiles);
MakeArtLive(tiles);
}
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;
}