-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.c
69 lines (53 loc) · 1.67 KB
/
day1.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>
#include <string.h>
#include "day1.h"
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("No input masses provided\n");
return 0;
}
int len = argc - 1;
int *numbers = read_numbers(len, argv + 1);
printf(" > running basic equation...\n");
int basic_fuel = basic_equation(len, numbers);
printf("---\n > total fuel required: %d\n\n", basic_fuel);
printf(" > running rocket equation...");
int total_fuel = rocket_equation(len, numbers);
printf("---\n > total fuel required: %d\n\n", total_fuel);
return 0;
}
int *read_numbers(int len, char *numbers[]) {
int *results = (int *)malloc(len * sizeof(int));
for (int i = 0; i < len; i++) {
results[i] = atoi(numbers[i]);
}
return results;
}
int basic_equation(int len, int masses[]) {
int total_fuel = 0;
for (int i = 0; i < len; i++) {
int fuel = fuel_required(masses[i]);
printf("%d kg requires %d kg of fuel\n", masses[i], fuel);
total_fuel += fuel;
}
return total_fuel;
}
int rocket_equation(int len, int masses[]) {
int total_fuel = 0;
for (int i = 0; i < len; i++) {
int fuel = 0;
int last_amount = fuel_required(masses[i]);
while (last_amount > 0) {
fuel += last_amount;
last_amount = fuel_required(last_amount);
}
printf("%d kg requires %d kg of fuel\n", masses[i], fuel);
total_fuel += fuel;
}
return total_fuel;
}
int fuel_required(int mass) {
int result = (mass / 3) - 2;
return result > 0 ? result : 0;
}