-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_13.cpp
132 lines (116 loc) · 3.32 KB
/
day_13.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
//---------------------------------------------------------------------------//
//#include <string>
//#include <iostream>
//#include <sstream>
//#include <map>
//#include <unordered_map>
//#include <vector>
//#include <ranges>
//#include <fstream>
//#include <limits>
#include <set>
#include "day"
//---------------------------------------------------------------------------//
//#include "day_13.h"
//---------------------------------------------------------------------------//
namespace aoc::YEAR::DAY {
//---------------------------------------------------------------------------//
// template<class T>
// constexpr auto Range ( T a, T b ) {
// return std::views::iota ( a, b );
// }
//---------------------------------------------------------------------------//
struct Point {
int x, y;
};
//---------------------------------------------------------------------------//
auto load ( std::istream& fs ) {
//std::fstream fs ( file );
std::vector<Point> data;
std::vector<std::tuple<char, int>> fold;
std::string line;
// punkty
while ( fs.good() ) {
std::getline ( fs, line );
if ( !line.empty() ) {
std::stringstream ss ( line );
int x, y;
char c;
ss >> x >> c >> y;
data.push_back ( {x, y} );
} else {
break;
}
}
// zlozenia
while ( fs.good() ) {
std::getline ( fs, line );
if ( !line.empty() ) {
char c;
int v;
std::stringstream ss ( line );
ss.seekg ( 11 ) >> c;
ss.seekg ( 13 ) >> v;
fold.push_back ( {c, v} );
}
}
return std::tuple{data, fold};
}
//---------------------------------------------------------------------------//
void Task_1 ( std::istream& puzzle_input ) {
auto ans = 0;
auto [data, fold] = load ( puzzle_input );
auto [a, v] = fold[0];
for ( auto& p : data ) {
if ( a == 'x' ) {
if ( p.x > v ) {
p.x = 2 * v - p.x;
}
} else {
if ( p.y > v ) {
p.y = 2 * v - p.y;
}
}
}
std::set<int> paper;
for ( auto& [x, y] : data ) {
paper.insert ( ( x << 16 ) | y );
}
ans = paper.size();
std::cout << __FUNCTION__ << ": " << ans << '\n';
}
//---------------------------------------------------------------------------//
void Task_2 ( std::istream& puzzle_input ) {
auto [data, fold] = load ( puzzle_input );
for ( auto [a, v] : fold )
for ( auto& p : data ) {
if ( a == 'x' ) {
if ( p.x > v ) {
p.x = 2 * v - p.x;
}
} else {
if ( p.y > v ) {
p.y = 2 * v - p.y;
}
}
}
auto minx = 0;
auto maxx = 0;
auto miny = 0;
auto maxy = 0;
for ( auto[x, y] : data ) {
minx = std::min ( minx, x );
maxx = std::max ( maxx, x );
miny = std::min ( miny, y );
maxy = std::max ( maxy, y );
}
std::vector<std::string> img ( maxy + 1 );
for ( auto& s : img ) s.resize ( maxx + 1, ' ' );
for ( auto& [x, y] : data ) {
img[y][x] = '#';
}
for ( auto& s : img )
std::cout << s << '\n';
std::cout << __FUNCTION__ << ":" << '\n';
}
}