aboutsummaryrefslogtreecommitdiff
path: root/2025
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-12-09 21:18:20 +0100
committerThomas Schmucker <ts@its1.de>2025-12-09 21:18:20 +0100
commitd88a2248710f3b9a9a2a6e16707f20b5662d72ab (patch)
tree96c35707161ed543a5624185d87c6eba25a1e06a /2025
parentd89b7d7fcf5a42bc5a7e0eef1304c73a09929611 (diff)
downloadadvent-of-code-d88a2248710f3b9a9a2a6e16707f20b5662d72ab.tar.gz
advent-of-code-d88a2248710f3b9a9a2a6e16707f20b5662d72ab.tar.bz2
advent-of-code-d88a2248710f3b9a9a2a6e16707f20b5662d72ab.zip
aoc 2025, day 9, part 2
Diffstat (limited to '2025')
-rw-r--r--2025/src/day09.cpp88
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
9namespace { 9namespace {
10 10
11using Pos = tuple<long, long>; 11using Pos = tuple<long, long>; // (x, y)
12using Segment = tuple<Pos, Pos>; // (p1, p2)
12 13
13vector<Pos> 14vector<Pos>
14read_file(const filesystem::path& filename) 15read_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
50vector<Segment>
51build_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
60bool
61intersects(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
94bool
95contains(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
100void
101part2(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
51int 136int
@@ -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}