-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (107 loc) · 3.44 KB
/
main.cpp
File metadata and controls
126 lines (107 loc) · 3.44 KB
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
#include <iostream>
#include <chrono>
#include <fstream>
#include <unordered_set>
#include "../Utils/utils.h"
#include "../Utils/Vector.h"
using aoc_utils::Vector;
struct Folds {
int pos = 0;
char dir;
Folds(int position, char direction) : pos(position), dir(direction){}
};
void writeMapToFile(const std::vector<char>& map, int maxX, int maxY, const std::string& filename) {
std::ofstream outFile(filename);
if (!outFile.is_open()) {
exit(-1);
}
for (int row = 0; row < maxY; row++) {
for (int col = 0; col < maxX; col++) {
char currentChar = map[row * maxX + col];
outFile << currentChar;
}
outFile << "\n";
}
outFile.close();
std::cout << "Map written to " << filename << " successfully.\n";
}
void solvePuzzle(const std::vector<std::string>& puzzle, int stage){
int maxY = 0, maxX = 0;
std::vector<Folds> folds;
std::vector<Vector> points;
for(auto& line : puzzle){
if(line.empty()){
continue;
}
if(line.starts_with("fold")){
size_t equalsIdx = line.find('=');
int fold = std::stoi(line.substr(equalsIdx+1));
folds.emplace_back(fold, line[equalsIdx-1]);
continue;
}
points.emplace_back(line);
maxY = std::max(maxY, points.back().y);
maxX = std::max(maxX, points.back().x);
}
maxY++;
maxX++;
std::vector<char> map((maxY) * (maxX), '.');
for(auto& fold : folds){
for(auto& point : points){
int yAfterFold = point.y;
int xAfterFold = point.x;
if(fold.dir == 'y' && point.y > fold.pos){
yAfterFold = fold.pos - (point.y - fold.pos);
}
if(fold.dir == 'x' && point.x > fold.pos){
xAfterFold = fold.pos - (point.x - fold.pos);
}
point.y = yAfterFold;
point.x = xAfterFold;
}
if(stage == 1){
break;
}
}
for(auto& point : points){
map[point.y * (maxX) + point.x] = '#';
}
if(stage == 1){
int visiblePoints = 0;
for(int row = 0; row < maxY; row++){
for(int col = 0; col < maxX; col++){
if(map[row*maxX + col] == '#'){
visiblePoints++;
}
}
}
std::cout << visiblePoints << "\n";
}
if(stage == 2){
writeMapToFile(map, maxX, maxY, "output_map.txt");
}
}
void stage1(const std::vector<std::string>& puzzle) {
solvePuzzle(puzzle, 1);
}
void stage2(const std::vector<std::string>& puzzle){
solvePuzzle(puzzle, 2);
}
int main(){
std::vector<std::string> puzzle;
std::string input = "example";
input = "input";
aoc_utils::load_puzzle(input, puzzle);
std::vector<std::string> curr;
curr.resize(2);
auto start = std::chrono::high_resolution_clock::now();
stage1(puzzle);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Stage 1 took " << elapsed.count() << " seconds\n";
start = std::chrono::high_resolution_clock::now();
stage2(puzzle);
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "Stage 2 took " << elapsed.count() << " seconds\n";
}