diff --git a/Day 13/part1.cpp.cpp b/Day 13/part1.cpp.cpp new file mode 100644 index 0000000..0698f8b --- /dev/null +++ b/Day 13/part1.cpp.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include + +using namespace std; + +set> flip_about(set> input_points, bool x_flip, int value); + +int main(int argc, char* argv[]) +{ + string line; + bool coord_input = true; + set> coords; + vector> folds; + while(getline(cin, line)) + { + if (0 == line.size()) + { + coord_input = false; + continue; + } + + if (coord_input) + { + string::size_type n = line.find(','); + pair coord(stoi(line.substr(0,n)), stoi(line.substr(n+1))); + coords.insert(coord); + } + else + { + string::size_type n = line.find_last_of(' '); + line = line.substr(n+1); + pair fold(line[0]=='x', stoi(line.substr(2))); + folds.push_back(fold); + } + } + + + coords = flip_about(coords, folds[0].first, folds[0].second); + + cout << coords.size() << endl; +} + +set> flip_about(set> input_points, bool x_flip, int value) +{ + set> output_points; + set>::iterator it = input_points.begin(); + for(; it != input_points.end(); ++it) + { + if (x_flip) + { + if (it->first > value) + { + pair flipped_coord(2*value-it->first, it->second); + output_points.insert(flipped_coord); + } + else + { + output_points.insert(*it); + } + } + else + { + if (it->second > value) + { + pair flipped_coord(it->first, 2*value-it->second); + output_points.insert(flipped_coord); + } + else + { + output_points.insert(*it); + } + } + } + + return output_points; +} \ No newline at end of file diff --git a/Day 13/part2.cpp b/Day 13/part2.cpp new file mode 100644 index 0000000..f85fe4e --- /dev/null +++ b/Day 13/part2.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include + +using namespace std; + +set> flip_about(set> input_points, bool x_flip, int value); + +int main(int argc, char* argv[]) +{ + string line; + bool coord_input = true; + set> coords; + vector> folds; + while(getline(cin, line)) + { + if (0 == line.size()) + { + coord_input = false; + continue; + } + + if (coord_input) + { + string::size_type n = line.find(','); + pair coord(stoi(line.substr(0,n)), stoi(line.substr(n+1))); + coords.insert(coord); + } + else + { + string::size_type n = line.find_last_of(' '); + line = line.substr(n+1); + pair fold(line[0]=='x', stoi(line.substr(2))); + folds.push_back(fold); + } + } + + for (unsigned int i = 0; i < folds.size(); i++) + { + coords = flip_about(coords, folds[i].first, folds[i].second); + } + + int xmax = 0, ymax = 0; + for (set>::iterator it = coords.begin(); it != coords.end(); ++it) + { + if (it->first > xmax) + { + xmax = it->first; + } + if (it->second > ymax) + { + ymax = it->second; + } + } + ++xmax; + ++ymax; + + bool** marked = new bool*[xmax]; + for(int x = 0; x < xmax; x++) + { + marked[x] = new bool[ymax]; + for(int y = 0; y < ymax; y++) + { + marked[x][y] = false; + } + } + + for (set>::iterator it = coords.begin(); it != coords.end(); ++it) + { + marked[it->first][it->second] = true; + } + + for (int y = 0; y < ymax; y++) + { + for (int x = 0; x < xmax; x++) + { + cout << (marked[x][y]?"#":" "); + } + cout << endl; + } + + for(int x = 0; x < xmax; x++) + { + delete[] marked[x]; + } + delete[] marked; +} + +set> flip_about(set> input_points, bool x_flip, int value) +{ + set> output_points; + set>::iterator it = input_points.begin(); + for(; it != input_points.end(); ++it) + { + if (x_flip) + { + if (it->first > value) + { + pair flipped_coord(2*value-it->first, it->second); + output_points.insert(flipped_coord); + } + else + { + output_points.insert(*it); + } + } + else + { + if (it->second > value) + { + pair flipped_coord(it->first, 2*value-it->second); + output_points.insert(flipped_coord); + } + else + { + output_points.insert(*it); + } + } + } + + return output_points; +} \ No newline at end of file