-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.cpp
120 lines (107 loc) · 3.06 KB
/
day13.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
#include "AOC_Solver.h"
#include <algorithm>
#include <iostream>
struct Point
{
int x, y;
bool operator==(const Point& rhs) const {
return rhs.x == x && rhs.y == y;
}
};
uint64_t aoc::day13::part_1(std::vector<std::string>& input) {
uint32_t space=std::find(input.begin(), input.end(), "")-input.begin();
//std::cout << space<<"\n";
std::vector<Point>points{};
points.resize(space);
for(int i=0;i<space;++i)
{
int delim = input[i].find_first_of(",");
int x = std::stoi(input[i].substr(0, delim));
int y = std::stoi(input[i].substr(delim+1));
//std::cout << x<<" "<<y<<"\n";
points[i] = { x,y };
}
char direction = input[space + 1][11];
int position = std::stoi(input[space + 1].substr(13));
//std::cout << direction << "=" << position << "\n";
for(auto it=points.begin();it!=points.end();)
{
bool erase = false;
if((direction=='y'&&it->y>position) || (direction == 'x' && it->x > position))
{
int point_pos = direction == 'y' ? it->y : it->x;
int new_pos = position - (point_pos - position);
Point new_point = { direction == 'y'?it->x:new_pos, direction == 'y'?new_pos:it->y};
if (std::any_of(points.begin(), points.end(), [&](Point& p){return new_point.x==p.x&&new_point.y==p.y;}))
erase = true;
else if (direction == 'y')
it->y = new_pos;
else
it->x = new_pos;
}
if (erase)
it = points.erase(it);
else
++it;
}
return points.size();
}
uint64_t aoc::day13::part_2(std::vector<std::string>& input) {
uint32_t space = std::find(input.begin(), input.end(), "") - input.begin();
//std::cout << space<<"\n";
std::vector<Point>points{};
points.resize(space);
for (int i = 0; i < space; ++i)
{
int delim = input[i].find_first_of(",");
int x = std::stoi(input[i].substr(0, delim));
int y = std::stoi(input[i].substr(delim + 1));
//std::cout << x<<" "<<y<<"\n";
points[i] = { x,y };
}
int fold_x{}, fold_y{};
for(int i=space+1;i<input.size();++i)
{
char direction = input[i][11];
int position = std::stoi(input[i].substr(13));
if (direction == 'y')fold_y = position;
else fold_x = position;
for (auto it = points.begin(); it != points.end();)
{
bool erase = false;
if ((direction == 'y' && it->y > position) || (direction == 'x' && it->x > position))
{
int point_pos = direction == 'y' ? it->y : it->x;
int new_pos = position - (point_pos - position);
Point new_point = { direction == 'y' ? it->x : new_pos, direction == 'y' ? new_pos : it->y };
if (std::any_of(points.begin(), points.end(), [&](Point& p) {return new_point.x == p.x && new_point.y == p.y; }))
erase = true;
else if (direction == 'y')
it->y = new_pos;
else
it->x = new_pos;
}
if (erase)
it = points.erase(it);
else
++it;
}
}
std::string result{};
for(int y=0;y<fold_y;++y)
{
for(int x=0;x<fold_x;++x)
{
Point new_point = { x,y };
if (std::any_of(points.begin(), points.end(), [&](Point& p) {return new_point.x == p.x && new_point.y == p.y; }))
result+='#';
else
result += ' ';
}
result += "\n";
}
#if !NDEBUG
std::cout << result;
#endif
return 0;
}