-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_10a.cpp
91 lines (79 loc) · 3.42 KB
/
day_10a.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
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <chrono>
#include <thread>
#include <memory>
#include <numeric>
#include <regex>
struct Point {
int x;
int y;
Point(int x, int y) : x(x), y(y){}
Point operator + (const Point& p) const {
return {this->x + p.x, this->y + p.y};
}
Point operator - (const Point& p) const {
return {this->x - p.x, this->y - p.y};
}
};
inline long long calcArea(const std::vector<Point>& positions) {
const auto min_x = std::min_element(std::begin(positions), std::end(positions), [](const Point& p1, const Point& p2) { return p1.x < p2.x; })->x;
const auto max_x = std::max_element(std::begin(positions), std::end(positions), [](const Point& p1, const Point& p2) { return p1.x < p2.x; })->x;
const auto min_y = std::min_element(std::begin(positions), std::end(positions), [](const Point& p1, const Point& p2) { return p1.y < p2.y; })->y;
const auto max_y = std::max_element(std::begin(positions), std::end(positions), [](const Point& p1, const Point& p2) { return p1.y < p2.y; })->y;
const long long area = (max_y - min_y) * (max_y - min_y);
return area > 0 ? area : std::numeric_limits<long long>::max();
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_10_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
const std::regex pattern(R"(position=<\s*(-?[0-9]+), \s*(-?[0-9]+)> velocity=<\s*(-?[0-9]+), \s*(-?[0-9]+)>)");
std::vector<Point> positions;
std::vector<Point> velocities;
while(std::getline(file, line)) {
std::smatch match;
std::regex_search(line, match, pattern);
positions.emplace_back(stoi(match[1]), stoi(match[2]));
velocities.emplace_back(stoi(match[3]), stoi(match[4]));
}
long long area = calcArea(positions);
long long max_area = std::pow(positions.size(), 2);
long long prev_area = std::numeric_limits<long long>::max();
while (area > max_area || area < prev_area) {
std::transform(std::begin(positions), std::end(positions),
std::begin(velocities), std::begin(positions),
[](const auto& p, const auto& v) { return p + v ;} );
prev_area = area;
area = calcArea(positions);
}
std::transform(std::begin(positions), std::end(positions),
std::begin(velocities), std::begin(positions),
[](const auto& p, const auto& v) { return p - v ;} );
const auto min_x = std::min_element(std::begin(positions), std::end(positions), [](const Point& p1, const Point& p2) { return p1.x < p2.x; })->x;
const auto max_x = std::max_element(std::begin(positions), std::end(positions), [](const Point& p1, const Point& p2) { return p1.x < p2.x; })->x;
const auto min_y = std::min_element(std::begin(positions), std::end(positions), [](const Point& p1, const Point& p2) { return p1.y < p2.y; })->y;
const auto max_y = std::max_element(std::begin(positions), std::end(positions), [](const Point& p1, const Point& p2) { return p1.y < p2.y; })->y;
std::vector<std::vector<char>> canvas(std::abs(max_y - min_y) + 1, std::vector<char>(std::abs(max_x - min_x) + 1, '.'));
for (const auto& p : positions) {
canvas[p.y - min_y][p.x - min_x] = '#';
}
for (int i = 0; i <= max_y - min_y; i++) {
for (int j = 0; j <= max_x - min_x ; j++) {
std::cout << canvas[i][j];
}
std::cout << '\n';
}
return 0;
}