-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
107 lines (87 loc) · 2.26 KB
/
code.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
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>
#include <filesystem>
#include <string>
std::set<std::pair<int,int>> fold(std::set<std::pair<int,int>> data,char axis, int coord) {
std::set<std::pair<int,int>> dots;
if ( axis == 'x' ) {
for (auto dot : data) {
if (dot.first > coord) {
dots.insert({coord - abs(dot.first-coord),dot.second});
} else {
dots.insert(dot);
}
}
} else {
for (auto dot : data) {
if (dot.second > coord) {
dots.insert({dot.first,coord - abs(dot.second-coord)});
} else {
dots.insert(dot);
}
}
}
return dots;
}
int main () {
if ( !std::filesystem::exists("input") ){
std::cout << "input file does not exist" << std::endl;
return 1;
}
std::ifstream input("input");
/* --- Part 1 --- */
std::set<std::pair<int,int>> data;
std::vector<std::pair<char,size_t>> folds;
std::string line;
while ( std::getline(input, line) && !line.empty()) {
std::replace( line.begin(), line.end(), ',', ' ');
std::istringstream linestream(line);
int x,y;
linestream >> x >> y;
data.insert({x,y});
}
while ( !input.eof() ) {
std::string line;
char fold_to;
size_t number;
std::getline(input, line);
line.replace(line.find("fold along "),11,"");
line.replace(line.find("="),1," ");
std::istringstream linestream(line);
linestream >> fold_to >> number;
folds.push_back({fold_to,number});
}
/* --- Part 1 --- */
std::set<std::pair<int,int>> part1 = fold(data,folds[0].first,folds[0].second);
std::cout << "Part1: " << part1.size() << std::endl;
/* --- Part 2 --- */
for( auto f : folds ) {
data = fold(data,f.first,f.second);
}
int x_size = 0, y_size = 0;
for (auto pair : data) {
if ( pair.first > x_size ) { x_size = pair.first; }
if ( pair.second > y_size ) { y_size = pair.second; }
}
x_size++;
y_size++;
std::vector<std::vector<char>> paper(y_size,std::vector<char> (x_size, '.'));
for (auto pair : data) {
paper[pair.second][pair.first] = '#';
}
std::cout << "Part2:" << std::endl;
for ( auto line : paper ) {
for ( auto point : line ) {
if ( point == '#' ) {
std::cout << "\033[0;31m#\033[0m";
} else {
std::cout << "\033[0;30m.\033[0m";
}
}
std::cout << std::endl;
}
return 0;
}