-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.c
69 lines (54 loc) · 1.33 KB
/
particles.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
#include <stdio.h>
#include <stdlib.h>
struct Vector3 {
int x, y, z;
};
struct Particle {
int id;
struct Vector3 pos, vel, acc;
};
int manhattan(const struct Vector3 vec) {
return abs(vec.x) + abs(vec.y) + abs(vec.z);
}
int relation(const int a, const int b) {
if(a > b) return +1;
if(a < b) return -1;
return 0;
}
int CompareParticles(const void *A_void, const void *B_void) {
const struct Particle *A = A_void;
const struct Particle *B = B_void;
int a = manhattan(A->acc);
int b = manhattan(B->acc);
int cmp = relation(a, b);
if(cmp != 0) return cmp;
a = manhattan(A->vel);
b = manhattan(B->vel);
cmp = relation(a, b);
if(cmp != 0) return cmp;
a = manhattan(A->pos);
b = manhattan(B->pos);
return relation(a, b);
}
#define MAX_PARTICLES 1024
struct Particle part[MAX_PARTICLES];
int pnum = 0;
int readline(void) {
int matches = scanf(
"p=<%d,%d,%d>, v=<%d,%d,%d>, a=<%d,%d,%d> ",
&part[pnum].pos.x, &part[pnum].pos.y, &part[pnum].pos.z,
&part[pnum].vel.x, &part[pnum].vel.y, &part[pnum].vel.z,
&part[pnum].acc.x, &part[pnum].acc.y, &part[pnum].acc.z
);
return matches == 9;
}
int main(void) {
while(readline()) {
part[pnum].id = pnum;
++pnum;
}
printf("> %d particles read\n", pnum);
qsort(part, pnum, sizeof(struct Particle), &CompareParticles);
printf("particle %d\n", part[0].id);
return 0;
}