aboutsummaryrefslogtreecommitdiff
path: root/2018/src/day06.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-11-16 13:23:34 +0100
committerThomas Schmucker <ts@its1.de>2025-11-16 13:23:34 +0100
commit8c96639ec1f6757570510fc27f1c5fabece35eaf (patch)
tree3a2b2146b6e8a353fa4c5edd105790320cce2b26 /2018/src/day06.cpp
parentbdd35a7bee7f23c912d0c453abc263805d8d8ccf (diff)
downloadadvent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.tar.gz
advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.tar.bz2
advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.zip
aoc 2018, days 1-11
Diffstat (limited to '2018/src/day06.cpp')
-rw-r--r--2018/src/day06.cpp152
1 files changed, 152 insertions, 0 deletions
diff --git a/2018/src/day06.cpp b/2018/src/day06.cpp
new file mode 100644
index 0000000..8701cbe
--- /dev/null
+++ b/2018/src/day06.cpp
@@ -0,0 +1,152 @@
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4#include <map>
5#include <set>
6#include <tuple>
7
8using namespace std;
9
10namespace {
11
12using Point = tuple<int, int>;
13
14set<Point>
15read_file(const filesystem::path& filename)
16{
17 ifstream file{ filename };
18 set<Point> points;
19
20 int x{};
21 int y{};
22 char sep{};
23
24 while ( file >> x >> sep >> y ) {
25 points.insert({ x, y });
26 }
27
28 return points;
29}
30
31void
32getBoundingBox(const set<Point>& points, Point& topLeft, Point& bottomRight)
33{
34 auto minX = numeric_limits<int>::max();
35 auto minY = numeric_limits<int>::max();
36 auto maxX = numeric_limits<int>::min();
37 auto maxY = numeric_limits<int>::min();
38
39 for ( const auto [x, y]: points ) {
40 minX = min(minX, x);
41 minY = min(minY, y);
42 maxX = max(maxX, x);
43 maxY = max(maxY, y);
44 }
45
46 topLeft = { minX, minY };
47 bottomRight = { maxX, maxY };
48}
49
50int
51manhattanDistance(const Point& lhs, const Point& rhs)
52{
53 int distance = abs(get<0>(lhs) - get<0>(rhs)) + abs(get<1>(lhs) - get<1>(rhs));
54
55 return distance;
56}
57
58void
59part1(const set<Point>& points)
60{
61 Point topLeft{};
62 Point bottomRight{};
63 getBoundingBox(points, topLeft, bottomRight);
64
65 const auto [minX, minY] = topLeft;
66 const auto [maxX, maxY] = bottomRight;
67
68 map<Point, int> areaCount;
69 set<Point> infinite;
70
71 static const int offset = 1;
72
73 for ( auto posX = minX - offset; posX <= maxX + offset; ++posX ) {
74 for ( auto posY = minY - offset; posY <= maxY + offset; ++posY ) {
75 int bestDistance = numeric_limits<int>::max();
76 Point bestPoint{};
77 bool tie = false;
78
79 for ( const auto& point: points ) {
80 int distance = manhattanDistance(point, { posX, posY });
81 if ( distance < bestDistance ) {
82 bestDistance = distance;
83 bestPoint = point;
84 tie = false;
85 }
86 else if ( distance == bestDistance ) {
87 tie = true;
88 }
89 }
90
91 if ( !tie ) {
92 areaCount[bestPoint]++;
93
94 if ( posX < minX || posX > maxX || posY < minY || posY > maxY ) {
95 infinite.insert(bestPoint);
96 }
97 }
98 }
99 }
100
101 int maxSize = 0;
102 for ( const auto& [point, size]: areaCount ) {
103 if ( infinite.contains(point) ) {
104 continue;
105 }
106 maxSize = max(maxSize, size);
107 }
108
109 cout << "Part 1: " << maxSize << '\n';
110}
111
112void
113part2(const set<Point>& points, const int threshold)
114{
115 Point topLeft{};
116 Point bottomRight{};
117 getBoundingBox(points, topLeft, bottomRight);
118
119 const auto [minX, minY] = topLeft;
120 const auto [maxX, maxY] = bottomRight;
121
122 static const int offset = 100;
123
124 unsigned int regionSize = 0;
125
126 for ( auto posX = minX - offset; posX <= maxX + offset; ++posX ) {
127 for ( auto posY = minY - offset; posY <= maxY + offset; ++posY ) {
128 int sum = 0;
129
130 for ( const auto& point: points ) {
131 int distance = manhattanDistance(point, { posX, posY });
132 sum += distance;
133 }
134
135 if ( sum < threshold ) {
136 ++regionSize;
137 }
138 }
139 }
140
141 cout << "Part 2: " << regionSize << '\n';
142}
143
144} // namespace
145
146int
147main()
148{
149 auto points = read_file("data/day06.txt");
150 part1(points);
151 part2(points, 10000);
152}