aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/day11.cpp87
1 files changed, 21 insertions, 66 deletions
diff --git a/src/day11.cpp b/src/day11.cpp
index baa1d9e..e1a0882 100644
--- a/src/day11.cpp
+++ b/src/day11.cpp
@@ -19,36 +19,6 @@ read_file(string_view filename)
19 return data; 19 return data;
20} 20}
21 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>> 22vector<tuple<long, long>>
53find_points(const vector<string>& input) 23find_points(const vector<string>& input)
54{ 24{
@@ -68,33 +38,8 @@ find_points(const vector<string>& input)
68} 38}
69 39
70void 40void
71solve(const vector<tuple<long, long>>& points) 41find_empty_rows_and_cols(const vector<string>& input, set<long>& cols, set<long>& rows)
72{ 42{
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 ) { 43 for ( size_t row = 0; row != input.size(); ++row ) {
99 const auto& line = input[row]; 44 const auto& line = input[row];
100 if ( line.find('#') == line.npos ) { 45 if ( line.find('#') == line.npos ) {
@@ -115,31 +60,41 @@ find_empty_rows_ans_cols(const vector<string>& input, set<long>& cols, set<long>
115 } 60 }
116} 61}
117 62
118void 63long
119part2() 64solve(const vector<string>& input, long scale)
120{ 65{
121 auto input = read_file("data/day11.txt");
122 auto points = find_points(input); 66 auto points = find_points(input);
123 67
124 set<long> cols; 68 set<long> cols;
125 set<long> rows; 69 set<long> rows;
126 70
127 find_empty_rows_ans_cols(input, cols, rows); 71 find_empty_rows_and_cols(input, cols, rows);
128 72
129 auto scale = 1000000 - 1;
130 for ( auto& point: points ) { 73 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; 74 const auto row = get<0>(point) + count_if(rows.begin(), rows.end(), [&](long row) { return row < get<0>(point); }) * (scale - 1);
132 const auto col = get<1>(point) + count_if(cols.begin(), cols.end(), [&](long col) { return col < get<1>(point); }) * scale; 75 const auto col = get<1>(point) + count_if(cols.begin(), cols.end(), [&](long col) { return col < get<1>(point); }) * (scale - 1);
133 76
134 point = { row, col }; 77 point = { row, col };
135 } 78 }
136 79
137 solve(points); 80 auto sum = 0L;
81 for ( size_t i = 0; i != points.size(); ++i ) {
82 for ( size_t j = i + 1; j != points.size(); ++j ) {
83 const auto& from = points[i];
84 const auto& to = points[j];
85
86 sum += abs(get<0>(from) - get<0>(to)) + abs(get<1>(from) - get<1>(to));
87 }
88 }
89
90 return sum;
138} 91}
139 92
140int 93int
141main() 94main()
142{ 95{
143 part1(); 96 const auto input = read_file("data/day11.txt");
144 part2(); 97
98 cout << "Part1: " << solve(input, 2) << endl;
99 cout << "Part2: " << solve(input, 1000000) << endl;
145} 100}