aboutsummaryrefslogtreecommitdiff
path: root/2025/src/day09.cpp
blob: 796bf07d213bb60db97d9b8df6602ade1dc0e785 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <filesystem>
#include <fstream>
#include <iostream>
#include <tuple>
#include <vector>

using namespace std;

namespace {

using Pos     = tuple<long, long>; // (x, y)
using Segment = tuple<Pos, Pos>;   // (p1, p2)

vector<Pos>
read_file(const filesystem::path& filename)
{
	ifstream file{ filename };

	vector<Pos> data;

	long x{};
	long y{};
	char skip{};

	while ( file >> x >> skip >> y ) {
		data.emplace_back(x, y);
	}
	return data;
}

void
part1(const vector<Pos>& data)
{
	auto area = numeric_limits<long>::min();

	for ( size_t i = 0; i != data.size(); ++i ) {
		for ( size_t j = i + 1; j != data.size(); ++j ) {
			const auto [x1, y1] = data[i];
			const auto [x2, y2] = data[j];

			const auto width  = abs(x1 - x2) + 1;
			const auto height = abs(y1 - y2) + 1;

			area = max(area, width * height);
		}
	}
	cout << "Part 1: " << area << '\n';
}

vector<Segment>
build_edges(const vector<Pos>& data)
{
	vector<Segment> edges;
	for ( size_t i = 0; i != data.size(); ++i ) {
		edges.emplace_back(data[i], data[(i + 1) % data.size()]);
	}
	return edges;
}

bool
intersects(const Segment& line, const tuple<long, long, long, long>& rect)
{
	const auto [start, end] = line;
	const auto [x1, y1]     = start;
	const auto [x2, y2]     = end;

	const auto [left, top, right, bottom] = rect;

	if ( x1 == x2 ) {
		if ( x1 <= left || x1 >= right ) {
			return false;
		}

		const auto min_y = min(y1, y2);
		const auto max_y = max(y1, y2);

		return max_y > top && min_y < bottom;
	}

	if ( y1 == y2 ) {
		if ( y1 <= top || y1 >= bottom ) {
			return false;
		}

		auto min_x = min(x1, x2);
		auto max_x = max(x1, x2);

		return max_x > left && min_x < right;
	}

	return false;
}

bool
contains(const vector<Segment>& poly, const tuple<long, long, long, long>& rect)
{
	return ranges::all_of(poly, [&](const auto& line) { return !intersects(line, rect); });
}

void
part2(const vector<Pos>& data)
{
	const auto poly = build_edges(data);

	auto max_area = numeric_limits<long>::min();

	for ( size_t i = 0; i != data.size(); ++i ) {
		for ( size_t j = i + 1; j != data.size(); ++j ) {
			const auto [x1, y1] = data[i];
			const auto [x2, y2] = data[j];

			const auto width  = abs(x1 - x2) + 1;
			const auto height = abs(y1 - y2) + 1;

			const auto area = width * height;

			if ( area <= max_area ) {
				continue;
			}

			const auto left   = min(x1, x2);
			const auto right  = max(x1, x2);
			const auto top    = min(y1, y2);
			const auto bottom = max(y1, y2);

			if ( contains(poly, { left, top, right, bottom }) ) {
				max_area = area;
			}
		}
	}
	cout << "Part 2: " << max_area << '\n';
}

} // namespace

int
main()
{
	auto data = read_file("data/day09.txt");
	part1(data);
	part2(data);
}