1
0
Fork 0
Advent2021/Day 13/part2.cpp

124 lines
2.9 KiB
C++

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <utility>
using namespace std;
set<pair<int,int>> flip_about(set<pair<int,int>> input_points, bool x_flip, int value);
int main(int argc, char* argv[])
{
string line;
bool coord_input = true;
set<pair<int,int>> coords;
vector<pair<bool,int>> folds;
while(getline(cin, line))
{
if (0 == line.size())
{
coord_input = false;
continue;
}
if (coord_input)
{
string::size_type n = line.find(',');
pair<int,int> 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<bool,int> 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<pair<int,int>>::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<pair<int,int>>::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<pair<int,int>> flip_about(set<pair<int,int>> input_points, bool x_flip, int value)
{
set<pair<int,int>> output_points;
set<pair<int,int>>::iterator it = input_points.begin();
for(; it != input_points.end(); ++it)
{
if (x_flip)
{
if (it->first > value)
{
pair<int,int> flipped_coord(2*value-it->first, it->second);
output_points.insert(flipped_coord);
}
else
{
output_points.insert(*it);
}
}
else
{
if (it->second > value)
{
pair<int,int> flipped_coord(it->first, 2*value-it->second);
output_points.insert(flipped_coord);
}
else
{
output_points.insert(*it);
}
}
}
return output_points;
}