-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11b.c
92 lines (76 loc) · 1.71 KB
/
day11b.c
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
// Licensed under the MIT License.
// Cosmic Expansion Part 2
#include <stdio.h>
#include <time.h>
#define DIMENSION 141
#define SCALE 1000000
static long long scan(long long vector[], int max, int galaxies)
{
int i = 1;
long long result = 0;
long long distance = 1;
long long passed = vector[0];
while (i <= max)
{
long long value = vector[i];
if (value)
{
result += ((2 * passed) - galaxies + value) * value * distance;
passed += value;
distance++;
}
else
{
distance += SCALE;
}
i++;
}
return result;
}
int main(void)
{
int n = 0;
int galaxies = 0;
int minI = DIMENSION;
int minJ = DIMENSION;
int maxI = 0;
int maxJ = 0;
long long x[DIMENSION] = { 0 };
long long y[DIMENSION] = { 0 };
char buffer[DIMENSION + 1];
clock_t start = clock();
while (fgets(buffer, sizeof buffer, stdin))
{
char current;
for (int j = 0; (current = buffer[j]); j++)
{
if (buffer[j] == '#')
{
y[n]++;
x[j]++;
galaxies++;
}
if (j > maxJ)
{
maxJ = j;
}
else if (j < minJ)
{
minJ = j;
}
}
if (y[n])
{
maxI = n;
if (n < minI)
{
minI = n;
}
}
n++;
}
long long result =
scan(y + minI, maxI - minI, galaxies) +
scan(x + minJ, maxJ - minJ, galaxies);
printf("11b %lld %lf\n", result, (double)(clock() - start) / CLOCKS_PER_SEC);
}