Finished both parts of advent of code day 5
This commit is contained in:
175
advent_of_code/d5/main.cpp
Normal file
175
advent_of_code/d5/main.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string FILE_NAME = "5.txt";
|
||||
|
||||
// First task: given line segments, find the number of intersected points
|
||||
// So don't count multiple intersections on the same point multiple times
|
||||
|
||||
|
||||
struct Point{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
using Line_Segment = pair<Point, Point>;
|
||||
|
||||
istream &operator>> ( istream &is, char const &c ) {
|
||||
istream::sentry sentry (is);
|
||||
if (sentry) { if ( is.peek() == c ) is.get(); else is.setstate(ios_base::failbit); }
|
||||
return is;
|
||||
}
|
||||
|
||||
istream &operator>> ( istream &is, char const *s ) {
|
||||
istream::sentry sentry (is);
|
||||
if (sentry) {
|
||||
for ( char c; is && (c = *s) != 0; ++s )
|
||||
if ( c == is.peek() ) is.get(); else is.setstate(ios_base::failbit);
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
istream &operator>> (istream &in, Point &p){
|
||||
return in >> p.x >> ',' >> p.y;
|
||||
}
|
||||
|
||||
ostream &operator<< (ostream &out, Point const &p){
|
||||
return out << p.x << ',' << p.y;
|
||||
}
|
||||
|
||||
bool operator< (const Point &l, const Point &r){
|
||||
return l.x < r.x || (l.x == r.x && l.y < r.y);
|
||||
}
|
||||
|
||||
istream &operator>> (istream &in, Line_Segment &l){
|
||||
return in >> l.first >> "->" >> l.second;
|
||||
}
|
||||
|
||||
ostream &operator<< (ostream &out, Line_Segment const &l){
|
||||
return out << l.first << " -> " << l.second;
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
ifstream file_stream;
|
||||
|
||||
file_stream.open(FILE_NAME);
|
||||
if(!file_stream.is_open()){
|
||||
cerr << "File " << FILE_NAME << " not found\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
cin.rdbuf(file_stream.rdbuf());
|
||||
|
||||
vector<Line_Segment> lines;
|
||||
while(cin){
|
||||
Line_Segment l;
|
||||
cin >> l;
|
||||
cin >> ws;
|
||||
lines.push_back(l);
|
||||
|
||||
cin.peek();
|
||||
}
|
||||
|
||||
// for(auto &l : lines){
|
||||
// cout << l << '\n';
|
||||
// }
|
||||
|
||||
int result = 0;
|
||||
map<Point, int> line_points;
|
||||
|
||||
for(auto const &line : lines){
|
||||
Point p1 = line.first, p2 = line.second;
|
||||
|
||||
if(p1.x == p2.x){
|
||||
if(p1.y > p2.y){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
for(int i = p1.y; i <= p2.y; ++i){
|
||||
line_points[p1]++;
|
||||
p1.y++;
|
||||
}
|
||||
} else if(p1.y == p2.y){
|
||||
if(p1.x > p2.x){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
for(int i = p1.x; i <= p2.x; ++i){
|
||||
line_points[p1]++;
|
||||
p1.x++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto const &lp : line_points){
|
||||
if(lp.second > 1){
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Result part 1: " << result << endl;
|
||||
|
||||
line_points.clear();
|
||||
result = 0;
|
||||
|
||||
for(auto const &line : lines){
|
||||
Point p1 = line.first, p2 = line.second;
|
||||
|
||||
if(p1.x == p2.x){
|
||||
if(p1.y > p2.y){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
for(int i = p1.y; i <= p2.y; ++i){
|
||||
line_points[p1]++;
|
||||
p1.y++;
|
||||
}
|
||||
} else if(p1.y == p2.y){
|
||||
if(p1.x > p2.x){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
for(int i = p1.x; i <= p2.x; ++i){
|
||||
line_points[p1]++;
|
||||
p1.x++;
|
||||
}
|
||||
} else{
|
||||
if(p1.x > p2.x){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
int dif = p2.x - p1.x;
|
||||
|
||||
if(p1.y > p2.y){
|
||||
for(int i = 0; i <= dif; ++i){
|
||||
line_points[p1]++;
|
||||
p1.y--;
|
||||
p1.x++;
|
||||
}
|
||||
} else{
|
||||
for(int i = 0; i <= dif; ++i){
|
||||
line_points[p1]++;
|
||||
p1.y++;
|
||||
p1.x++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto const &lp : line_points){
|
||||
if(lp.second > 1){
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Result part 2: " << result << endl;
|
||||
|
||||
file_stream.close();
|
||||
cout << flush;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user