-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
153 lines (137 loc) · 3.58 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <functional>
std::pair<char, size_t> extract_to_fold(const std::string &line)
{
std::stringstream ss(line);
std::string line_part;
getline(ss, line_part, '=');
char coord_1_name = line_part.back();
getline(ss, line_part);
return {coord_1_name, atoi(line_part.c_str())};
}
std::pair<std::set<std::pair<size_t, size_t>>, std::vector<std::pair<char, size_t>>> read_dots()
{
std::ifstream input("input.txt");
if (!input)
{
std::cerr << "Cannot open the input.txt" << std::endl;
exit(1);
}
std::set<std::pair<size_t, size_t>> dots;
std::string tmp;
while (getline(input, tmp))
{
if (!tmp.size())
{
break;
}
std::stringstream ss(tmp);
std::string n;
getline(ss, n, ',');
size_t x = atoi(n.c_str());
getline(ss, n);
size_t y = atoi(n.c_str());
dots.emplace(x, y);
}
std::vector<std::pair<char, size_t>> f;
while (getline(input, tmp))
{
auto f1 = extract_to_fold(tmp);
f.push_back(f1);
}
input.close();
return {dots, f};
}
void fold_y(std::set<std::pair<size_t, size_t>> &dots, const size_t coord)
{
std::set<std::pair<size_t, size_t>> new_dots;
for (const auto &dot : dots)
{
size_t y = dot.second > coord ? dot.second - 2 * (dot.second - coord) : dot.second;
new_dots.insert({dot.first, y});
}
dots = new_dots;
}
void fold_x(std::set<std::pair<size_t, size_t>> &dots, const size_t coord)
{
std::set<std::pair<size_t, size_t>> new_dots;
for (const auto &dot : dots)
{
size_t x = dot.first > coord ? dot.first - 2 * (dot.first - coord) : dot.first;
new_dots.insert({x, dot.second});
}
dots = new_dots;
}
void fold(std::set<std::pair<size_t, size_t>> &dots, const std::pair<char, size_t> &f)
{
switch (f.first)
{
case 'x':
fold_x(dots, f.second);
break;
case 'y':
fold_y(dots, f.second);
break;
default:
std::cout << "ERROR\n";
exit(1);
break;
}
}
void part_1()
{
const auto t_start = std::chrono::high_resolution_clock::now();
auto [dots, f] = read_dots();
fold(dots, f.front());
const auto t_stop = std::chrono::high_resolution_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t_stop - t_start);
std::cout << "Part 1: " << dots.size() << '\n';
std::cout << "It took " << duration.count() << "us \n";
return;
}
void print(const std::set<std::pair<size_t, size_t>> &dots)
{
for (size_t j = 0; j <= (--dots.end())->second; j++)
{
for (size_t i = 0; i <= (--dots.end())->first; i++)
{
if (dots.find({i, j}) != dots.end())
{
std::cout << '#';
}
else
{
std::cout << '.';
}
}
std::cout << '\n';
}
}
void part_2()
{
const auto t_start = std::chrono::high_resolution_clock::now();
auto [dots, f] = read_dots();
for (const auto &c : f)
{
fold(dots, c);
}
std::cout << "Part 2: " << '\n';
print(dots);
const auto t_stop = std::chrono::high_resolution_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t_stop - t_start);
std::cout << "It took " << duration.count() << "us \n";
return;
}
int main()
{
part_1();
part_2();
return 0;
}