-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
173 lines (161 loc) Β· 4.29 KB
/
main.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
int main(int argc, char* argv[])
{
Moon moons_init[4];
Moon moons[4];
char* file_name = argv[1];
FILE* file = fopen(file_name, "r");
char line[32];
for (int i = 0; i < 4; i++) {
fgets(line, sizeof(line), file);
initialize_moon(line, &moons_init[i]);
initialize_moon(line, &moons[i]);
}
fclose(file);
// int num_steps = 1000;
// print_moons(moons);
// for (int i = 0; i < num_steps; i++) {
// update_velocities(moons);
// update_positions(moons);
// }
// print_moons(moons);
// int energy = calculate_energy(moons);
// printf("Total energy: %d\n", energy);
long intervals[3];
long ct;
for (int dim = 0; dim < 3; dim++) {
ct = 0;
do
{
update_dimension_velocities(moons, dim);
update_dimension_positions(moons, dim);
ct++;
} while (compare_moons(moons, moons_init, dim) == 0);
intervals[dim] = ct;
}
printf("Intervals: [%ld, %ld, %ld] ", intervals[0], intervals[1], intervals[2]);
printf("LCM: %ld\n", least_common_multiple(intervals[0], intervals[1], intervals[2]));
return 0;
}
void print_moons(Moon* moons)
{
for (int i = 0; i < 4; i++) {
printf("pos=<x=%4d, y=%4d, z=%4d>, ", moons[i].position[0], moons[i].position[1], moons[i].position[2]);
printf("vel=<x=%4d, y=%4d, z=%4d>\n", moons[i].velocity[0], moons[i].velocity[1], moons[i].velocity[2]);
}
}
long least_common_multiple(long a, long b, long c)
{
long lcm = a * b * c;
long temp_lcm;
long max;
if (a >= b && a >= c) {
max = a;
} else if (b >= c) {
max = b;
} else {
max = c;
}
for (long factor = max; factor > 1; factor--) {
if (a % factor != 0 && b % factor != 0 && c % factor != 0) {
continue;
}
temp_lcm = lcm / factor;
if (temp_lcm % a == 0 && temp_lcm % b == 0 && temp_lcm % c == 0) {
lcm = temp_lcm;
}
}
return lcm;
}
int compare_moons(Moon* moons, Moon* moons_init, int dim)
{
for (int i = 0; i < 4; i++) {
if (moons[i].position[dim] != moons_init[i].position[dim]) {
return 0;
}
if (moons[i].velocity[dim] != moons_init[i].velocity[dim]) {
return 0;
}
}
return 1;
}
int potential_energy(Moon* moon) {
int energy = 0;
for (int dim = 0; dim < 3; dim++) {
energy += abs(moon->position[dim]);
}
return energy;
}
int kinetic_energy(Moon* moon) {
int energy = 0;
for (int dim = 0; dim < 3; dim++) {
energy += abs(moon->velocity[dim]);
}
return energy;
}
int calculate_energy(Moon* moons) {
int energy = 0;
for (int i = 0; i < 4; i++) {
energy += potential_energy(&moons[i]) * kinetic_energy(&moons[i]);
}
return energy;
}
void update_velocities(Moon* moons)
{
for (int dim = 0; dim < 3; dim++) {
update_dimension_velocities(moons, dim);
}
}
void update_dimension_velocities(Moon* moons, int dim)
{
for (int i = 0; i < 4; i++) {
for (int j = i; j < 4; j++) {
if (moons[i].position[dim] > moons[j].position[dim]) {
moons[i].velocity[dim]--;
moons[j].velocity[dim]++;
} else if (moons[i].position[dim] < moons[j].position[dim]){
moons[i].velocity[dim]++;
moons[j].velocity[dim]--;
}
}
}
}
void update_positions(Moon* moons)
{
for (int dim = 0; dim < 3; dim++) {
update_dimension_positions(moons, dim);
}
}
void update_dimension_positions(Moon* moons, int dim)
{
for (int i = 0; i < 4; i++) {
moons[i].position[dim] += moons[i].velocity[dim];
}
}
void initialize_moon(char* line, Moon* moon)
{
int i = 0;
int mult;
int value;
for (int dim = 0; dim < 3; dim++) {
mult = 1;
value = 0;
while (line[i] != '=') {
i++;
}
i++;
if (line[i] == '-') {
mult = -1;
i++;
}
while (line[i] != ',' && line[i] != '>') {
value = 10 * value + (line[i] - '0');
i++;
}
moon->position[dim] = mult * value;
moon->velocity[dim] = 0;
}
}