aboutsummaryrefslogtreecommitdiff
path: root/src/day11.cpp
blob: baa1d9e5afc156939335d865d8a9cab21883576c (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
143
144
145
#include <cmath>
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

vector<string>
read_file(string_view filename)
{
	fstream        input{ filename };
	vector<string> data;

	for ( string line; getline(input, line); ) {
		data.emplace_back(line);
	}

	return data;
}

vector<string>
expand(const vector<string>& input)
{
	vector<string> result;

	for ( const auto& line: input ) {
		if ( line.find('#') == line.npos ) {
			result.emplace_back(line);
		}
		result.emplace_back(line);
	}

	for ( size_t col = 0; col != result[0].size(); ++col ) {
		bool empty_col = true;
		for ( const auto& line: result ) {
			if ( line[col] == '#' ) {
				empty_col = false;
			}
		}
		if ( empty_col ) {
			for ( auto& line: result ) {
				line.insert(col, 1, '.');
			}
			++col;
		}
	}

	return result;
}

vector<tuple<long, long>>
find_points(const vector<string>& input)
{
	vector<tuple<long, long>> result;

	for ( size_t row = 0; row != input.size(); ++row ) {
		const auto& line = input[row];

		for ( size_t col = 0; col != line.size(); ++col ) {
			if ( line[col] == '#' ) {
				result.emplace_back(long(row), long(col));
			}
		}
	}

	return result;
}

void
solve(const vector<tuple<long, long>>& points)
{
	long sum = 0;
	for ( size_t i = 0; i != points.size(); ++i ) {
		for ( size_t j = i + 1; j != points.size(); ++j ) {
			const auto& from = points[i];
			const auto& to   = points[j];

			sum += abs(get<0>(from) - get<0>(to)) + abs(get<1>(from) - get<1>(to));
		}
	}
	cout << sum << endl;

}

void
part1()
{
	solve(find_points(expand(read_file("data/day11.txt"))));
}

void
find_empty_rows_ans_cols(const vector<string>& input, set<long>& cols, set<long>& rows)
{
	cols.clear();
	rows.clear();

	for ( size_t row = 0; row != input.size(); ++row ) {
		const auto& line = input[row];
		if ( line.find('#') == line.npos ) {
			rows.insert(long(row));
		}
	}

	for ( size_t col = 0; col != input[0].size(); ++col ) {
		bool empty_col = true;
		for ( const auto& line: input ) {
			if ( line[col] == '#' ) {
				empty_col = false;
			}
		}
		if ( empty_col ) {
			cols.insert(long(col));
		}
	}
}

void
part2()
{
	auto input  = read_file("data/day11.txt");
	auto points = find_points(input);

	set<long> cols;
	set<long> rows;

	find_empty_rows_ans_cols(input, cols, rows);

	auto scale = 1000000 - 1;
	for ( auto& point: points ) {
		const auto row = get<0>(point) + count_if(rows.begin(), rows.end(), [&](long row) { return row < get<0>(point); }) * scale;
		const auto col = get<1>(point) + count_if(cols.begin(), cols.end(), [&](long col) { return col < get<1>(point); }) * scale;

		point = { row, col };
	}

	solve(points);
}

int
main()
{
	part1();
	part2();
}