Day 9 submission
This commit is contained in:
+86
-86
@@ -1,86 +1,86 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Line
|
||||
{
|
||||
int x1,y1,x2,y2;
|
||||
|
||||
friend istream &operator>>( istream &input, Line &l ) {
|
||||
input >> l.x1 >> l.y1 >> l.x2 >> l.y2;
|
||||
return input;
|
||||
}
|
||||
};
|
||||
|
||||
inline constexpr int num_lines{500};
|
||||
inline constexpr int grid_size{1000};
|
||||
|
||||
int main()
|
||||
{
|
||||
int grid[grid_size*grid_size] = {0};
|
||||
Line line;
|
||||
set<int> dangerous_locations;
|
||||
|
||||
for(int i = 0; i < num_lines; i++)
|
||||
{
|
||||
cin >> line;
|
||||
|
||||
if (line.x1 == line.x2)
|
||||
{
|
||||
for(int y = min(line.y1, line.y2); y <= max(line.y1, line.y2); y++)
|
||||
{
|
||||
grid[line.x1 + grid_size*y]++;
|
||||
|
||||
if (grid[line.x1+y*grid_size] >= 2)
|
||||
{
|
||||
dangerous_locations.insert(line.x1+y*grid_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (line.y1 == line.y2)
|
||||
{
|
||||
for(int x = min(line.x1, line.x2); x <= max(line.x1, line.x2); x++)
|
||||
{
|
||||
grid[x + line.y1*grid_size]++;
|
||||
|
||||
if (grid[x+line.y1*grid_size] >= 2)
|
||||
{
|
||||
dangerous_locations.insert(x+line.y1*grid_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((line.y2-line.y1)/(line.x2-line.x1) > 0)
|
||||
{
|
||||
for(int x = min(line.x1, line.x2), y = min(line.y1, line.y2); x <= max(line.x1, line.x2); x++, y++)
|
||||
{
|
||||
grid[x + y*grid_size]++;
|
||||
|
||||
if (grid[x+y*grid_size] >= 2)
|
||||
{
|
||||
dangerous_locations.insert(x+y*grid_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int x = min(line.x1, line.x2), y = max(line.y1, line.y2); x <= max(line.x1, line.x2); x++, y--)
|
||||
{
|
||||
grid[x + y*grid_size]++;
|
||||
|
||||
if (grid[x+y*grid_size] >= 2)
|
||||
{
|
||||
dangerous_locations.insert(x+y*grid_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << dangerous_locations.size() << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Line
|
||||
{
|
||||
int x1,y1,x2,y2;
|
||||
|
||||
friend istream &operator>>( istream &input, Line &l ) {
|
||||
input >> l.x1 >> l.y1 >> l.x2 >> l.y2;
|
||||
return input;
|
||||
}
|
||||
};
|
||||
|
||||
inline constexpr int num_lines{500};
|
||||
inline constexpr int grid_size{1000};
|
||||
|
||||
int main()
|
||||
{
|
||||
int grid[grid_size*grid_size] = {0};
|
||||
Line line;
|
||||
set<int> dangerous_locations;
|
||||
|
||||
for(int i = 0; i < num_lines; i++)
|
||||
{
|
||||
cin >> line;
|
||||
|
||||
if (line.x1 == line.x2)
|
||||
{
|
||||
for(int y = min(line.y1, line.y2); y <= max(line.y1, line.y2); y++)
|
||||
{
|
||||
grid[line.x1 + grid_size*y]++;
|
||||
|
||||
if (grid[line.x1+y*grid_size] >= 2)
|
||||
{
|
||||
dangerous_locations.insert(line.x1+y*grid_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (line.y1 == line.y2)
|
||||
{
|
||||
for(int x = min(line.x1, line.x2); x <= max(line.x1, line.x2); x++)
|
||||
{
|
||||
grid[x + line.y1*grid_size]++;
|
||||
|
||||
if (grid[x+line.y1*grid_size] >= 2)
|
||||
{
|
||||
dangerous_locations.insert(x+line.y1*grid_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((line.y2-line.y1)/(line.x2-line.x1) > 0)
|
||||
{
|
||||
for(int x = min(line.x1, line.x2), y = min(line.y1, line.y2); x <= max(line.x1, line.x2); x++, y++)
|
||||
{
|
||||
grid[x + y*grid_size]++;
|
||||
|
||||
if (grid[x+y*grid_size] >= 2)
|
||||
{
|
||||
dangerous_locations.insert(x+y*grid_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int x = min(line.x1, line.x2), y = max(line.y1, line.y2); x <= max(line.x1, line.x2); x++, y--)
|
||||
{
|
||||
grid[x + y*grid_size]++;
|
||||
|
||||
if (grid[x+y*grid_size] >= 2)
|
||||
{
|
||||
dangerous_locations.insert(x+y*grid_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << dangerous_locations.size() << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user