-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11b.cpp
71 lines (62 loc) · 1.6 KB
/
11b.cpp
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
#include "lib/global.hpp"
#include "lib/os_linux.cpp"
#include "lib/format.cpp"
#include "lib/hashmap.cpp"
#include "lib/number.cpp"
#include "lib/algorithm.cpp"
s64 abss(s64 x) {
return x >= 0 ? x : -x;
}
int main() {
auto arr = array_load_from_file("input/11"_arr);
struct Pos {
s64 x, y;
};
Array_dyn<Pos> galaxies;
{
s64 x = 0, y = 0;
for (u8 c: arr) {
if (c == '#') {
array_push_back(&galaxies, {x, y});
} else if (c == '\n') {
x = 0;
y += 1;
continue;
}
x += 1;
}
}
{
array_sort_heap(&galaxies, [](Pos p) { return p.x; });
s64 expansion = 0;
s64 last_x = 0;
for (Pos& p: galaxies) {
if (p.x > last_x) {
expansion += p.x - last_x - 1;
last_x = p.x;
}
p.x += expansion * 999999;
}
}
{
array_sort_heap(&galaxies, [](Pos p) { return p.y; });
s64 expansion = 0;
s64 last_y = 0;
for (Pos& p: galaxies) {
if (p.y > last_y) {
expansion += p.y - last_y - 1;
last_y = p.y;
}
p.y += expansion * 999999;
}
}
s64 sum = 0;
for (s64 i = 0; i+1 < galaxies.size; ++i) {
for (s64 j = i+1; j < galaxies.size; ++j) {
Pos pi = galaxies[i];
Pos pj = galaxies[j];
sum += abss(pi.x - pj.x) + abss(pi.y - pj.y);
}
}
format_print("%d\n", sum);
}