-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12-part2.c
78 lines (63 loc) · 1.81 KB
/
day12-part2.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
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
int sgn(int val) {
return (val > 0) - (val < 0);
}
uint_fast64_t gcd(uint_fast64_t a, uint_fast64_t b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
uint_fast64_t lcm(uint_fast64_t a, uint_fast64_t b) {
return a / gcd(a, b) * b;
}
uint_fast64_t getPeriod(int *positions, int *velocities, int *initialPositions, size_t count) {
uint_fast64_t period = 0;
bool hasLooped = false;
while (!hasLooped) {
++period;
for (size_t i = 0; i < count; ++i) {
for (size_t j = 0; j < count; ++j) {
velocities[i] += sgn(positions[j] - positions[i]);
}
}
for (size_t i = 0; i < count; ++i) {
positions[i] += velocities[i];
}
hasLooped = true;
for (size_t i = 0; i < count && hasLooped; ++i) {
hasLooped = hasLooped && (positions[i] == initialPositions[i]) && (velocities[i] == 0);
}
}
return period;
}
int main() {
const size_t moonCount = 4;
int initialX[moonCount];
int initialY[moonCount];
int initialZ[moonCount];
int xPositions[moonCount];
int yPositions[moonCount];
int zPositions[moonCount];
int xVelocities[moonCount];
int yVelocities[moonCount];
int zVelocities[moonCount];
for (size_t i = 0; i < moonCount; ++i) {
scanf("<x=%d, y=%d, z=%d> ", &initialX[i], &initialY[i], &initialZ[i]);
xPositions[i] = initialX[i];
yPositions[i] = initialY[i];
zPositions[i] = initialZ[i];
xVelocities[i] = 0;
yVelocities[i] = 0;
zVelocities[i] = 0;
}
uint_fast64_t periodX = getPeriod(xPositions, xVelocities, initialX, moonCount);
uint_fast64_t periodY = getPeriod(yPositions, yVelocities, initialY, moonCount);
uint_fast64_t periodZ = getPeriod(zPositions, zVelocities, initialZ, moonCount);
uint_fast64_t period = lcm(periodX, periodY);
period = lcm(period, periodZ);
printf("%" PRIuFAST64 "\n", period);
return 0;
}