diff options
Diffstat (limited to '2025/src/day09.cpp')
| -rw-r--r-- | 2025/src/day09.cpp | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/2025/src/day09.cpp b/2025/src/day09.cpp index f46112a..796bf07 100644 --- a/2025/src/day09.cpp +++ b/2025/src/day09.cpp | |||
| @@ -8,7 +8,8 @@ using namespace std; | |||
| 8 | 8 | ||
| 9 | namespace { | 9 | namespace { |
| 10 | 10 | ||
| 11 | using Pos = tuple<long, long>; | 11 | using Pos = tuple<long, long>; // (x, y) |
| 12 | using Segment = tuple<Pos, Pos>; // (p1, p2) | ||
| 12 | 13 | ||
| 13 | vector<Pos> | 14 | vector<Pos> |
| 14 | read_file(const filesystem::path& filename) | 15 | read_file(const filesystem::path& filename) |
| @@ -46,6 +47,90 @@ part1(const vector<Pos>& data) | |||
| 46 | cout << "Part 1: " << area << '\n'; | 47 | cout << "Part 1: " << area << '\n'; |
| 47 | } | 48 | } |
| 48 | 49 | ||
| 50 | vector<Segment> | ||
| 51 | build_edges(const vector<Pos>& data) | ||
| 52 | { | ||
| 53 | vector<Segment> edges; | ||
| 54 | for ( size_t i = 0; i != data.size(); ++i ) { | ||
| 55 | edges.emplace_back(data[i], data[(i + 1) % data.size()]); | ||
| 56 | } | ||
| 57 | return edges; | ||
| 58 | } | ||
| 59 | |||
| 60 | bool | ||
| 61 | intersects(const Segment& line, const tuple<long, long, long, long>& rect) | ||
| 62 | { | ||
| 63 | const auto [start, end] = line; | ||
| 64 | const auto [x1, y1] = start; | ||
| 65 | const auto [x2, y2] = end; | ||
| 66 | |||
| 67 | const auto [left, top, right, bottom] = rect; | ||
| 68 | |||
| 69 | if ( x1 == x2 ) { | ||
| 70 | if ( x1 <= left || x1 >= right ) { | ||
| 71 | return false; | ||
| 72 | } | ||
| 73 | |||
| 74 | const auto min_y = min(y1, y2); | ||
| 75 | const auto max_y = max(y1, y2); | ||
| 76 | |||
| 77 | return max_y > top && min_y < bottom; | ||
| 78 | } | ||
| 79 | |||
| 80 | if ( y1 == y2 ) { | ||
| 81 | if ( y1 <= top || y1 >= bottom ) { | ||
| 82 | return false; | ||
| 83 | } | ||
| 84 | |||
| 85 | auto min_x = min(x1, x2); | ||
| 86 | auto max_x = max(x1, x2); | ||
| 87 | |||
| 88 | return max_x > left && min_x < right; | ||
| 89 | } | ||
| 90 | |||
| 91 | return false; | ||
| 92 | } | ||
| 93 | |||
| 94 | bool | ||
| 95 | contains(const vector<Segment>& poly, const tuple<long, long, long, long>& rect) | ||
| 96 | { | ||
| 97 | return ranges::all_of(poly, [&](const auto& line) { return !intersects(line, rect); }); | ||
| 98 | } | ||
| 99 | |||
| 100 | void | ||
| 101 | part2(const vector<Pos>& data) | ||
| 102 | { | ||
| 103 | const auto poly = build_edges(data); | ||
| 104 | |||
| 105 | auto max_area = numeric_limits<long>::min(); | ||
| 106 | |||
| 107 | for ( size_t i = 0; i != data.size(); ++i ) { | ||
| 108 | for ( size_t j = i + 1; j != data.size(); ++j ) { | ||
| 109 | const auto [x1, y1] = data[i]; | ||
| 110 | const auto [x2, y2] = data[j]; | ||
| 111 | |||
| 112 | const auto width = abs(x1 - x2) + 1; | ||
| 113 | const auto height = abs(y1 - y2) + 1; | ||
| 114 | |||
| 115 | const auto area = width * height; | ||
| 116 | |||
| 117 | if ( area <= max_area ) { | ||
| 118 | continue; | ||
| 119 | } | ||
| 120 | |||
| 121 | const auto left = min(x1, x2); | ||
| 122 | const auto right = max(x1, x2); | ||
| 123 | const auto top = min(y1, y2); | ||
| 124 | const auto bottom = max(y1, y2); | ||
| 125 | |||
| 126 | if ( contains(poly, { left, top, right, bottom }) ) { | ||
| 127 | max_area = area; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | cout << "Part 2: " << max_area << '\n'; | ||
| 132 | } | ||
| 133 | |||
| 49 | } // namespace | 134 | } // namespace |
| 50 | 135 | ||
| 51 | int | 136 | int |
| @@ -53,4 +138,5 @@ main() | |||
| 53 | { | 138 | { |
| 54 | auto data = read_file("data/day09.txt"); | 139 | auto data = read_file("data/day09.txt"); |
| 55 | part1(data); | 140 | part1(data); |
| 141 | part2(data); | ||
| 56 | } | 142 | } |
