aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/day11.cpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/day11.cpp b/src/day11.cpp
new file mode 100644
index 0000000..baa1d9e
--- /dev/null
+++ b/src/day11.cpp
@@ -0,0 +1,145 @@
1#include <cmath>
2#include <fstream>
3#include <iostream>
4#include <set>
5#include <string>
6#include <vector>
7using namespace std;
8
9vector<string>
10read_file(string_view filename)
11{
12 fstream input{ filename };
13 vector<string> data;
14
15 for ( string line; getline(input, line); ) {
16 data.emplace_back(line);
17 }
18
19 return data;
20}
21
22vector<string>
23expand(const vector<string>& input)
24{
25 vector<string> result;
26
27 for ( const auto& line: input ) {
28 if ( line.find('#') == line.npos ) {
29 result.emplace_back(line);
30 }
31 result.emplace_back(line);
32 }
33
34 for ( size_t col = 0; col != result[0].size(); ++col ) {
35 bool empty_col = true;
36 for ( const auto& line: result ) {
37 if ( line[col] == '#' ) {
38 empty_col = false;
39 }
40 }
41 if ( empty_col ) {
42 for ( auto& line: result ) {
43 line.insert(col, 1, '.');
44 }
45 ++col;
46 }
47 }
48
49 return result;
50}
51
52vector<tuple<long, long>>
53find_points(const vector<string>& input)
54{
55 vector<tuple<long, long>> result;
56
57 for ( size_t row = 0; row != input.size(); ++row ) {
58 const auto& line = input[row];
59
60 for ( size_t col = 0; col != line.size(); ++col ) {
61 if ( line[col] == '#' ) {
62 result.emplace_back(long(row), long(col));
63 }
64 }
65 }
66
67 return result;
68}
69
70void
71solve(const vector<tuple<long, long>>& points)
72{
73 long sum = 0;
74 for ( size_t i = 0; i != points.size(); ++i ) {
75 for ( size_t j = i + 1; j != points.size(); ++j ) {
76 const auto& from = points[i];
77 const auto& to = points[j];
78
79 sum += abs(get<0>(from) - get<0>(to)) + abs(get<1>(from) - get<1>(to));
80 }
81 }
82 cout << sum << endl;
83
84}
85
86void
87part1()
88{
89 solve(find_points(expand(read_file("data/day11.txt"))));
90}
91
92void
93find_empty_rows_ans_cols(const vector<string>& input, set<long>& cols, set<long>& rows)
94{
95 cols.clear();
96 rows.clear();
97
98 for ( size_t row = 0; row != input.size(); ++row ) {
99 const auto& line = input[row];
100 if ( line.find('#') == line.npos ) {
101 rows.insert(long(row));
102 }
103 }
104
105 for ( size_t col = 0; col != input[0].size(); ++col ) {
106 bool empty_col = true;
107 for ( const auto& line: input ) {
108 if ( line[col] == '#' ) {
109 empty_col = false;
110 }
111 }
112 if ( empty_col ) {
113 cols.insert(long(col));
114 }
115 }
116}
117
118void
119part2()
120{
121 auto input = read_file("data/day11.txt");
122 auto points = find_points(input);
123
124 set<long> cols;
125 set<long> rows;
126
127 find_empty_rows_ans_cols(input, cols, rows);
128
129 auto scale = 1000000 - 1;
130 for ( auto& point: points ) {
131 const auto row = get<0>(point) + count_if(rows.begin(), rows.end(), [&](long row) { return row < get<0>(point); }) * scale;
132 const auto col = get<1>(point) + count_if(cols.begin(), cols.end(), [&](long col) { return col < get<1>(point); }) * scale;
133
134 point = { row, col };
135 }
136
137 solve(points);
138}
139
140int
141main()
142{
143 part1();
144 part2();
145}