finish 01 and edit template
This commit is contained in:
parent
2e899f840e
commit
e1567cc6c5
2
01/_.h
2
01/_.h
|
@ -11,6 +11,8 @@
|
|||
#include <math.h>
|
||||
|
||||
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
|
||||
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
|
||||
#define ABS(X) ((X) >= 0 ? (X) : -(X))
|
||||
|
||||
char** str_split(char* a_str, const char a_delim);
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
gcc -g -Wall -Wextra -include_.h *.c
|
||||
gcc -g -Wall -Wextra -include_.h main.c
|
||||
|
|
53
01/main.c
53
01/main.c
|
@ -2,15 +2,54 @@
|
|||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
if (argc != 2)
|
||||
return 1;
|
||||
if (argc != 2)
|
||||
return 1;
|
||||
|
||||
char *input = argv[1];
|
||||
char **inputs = str_split(strdup(input), '\n');
|
||||
char *input = argv[1];
|
||||
char **inputs = str_split(strdup(input), '\n');
|
||||
|
||||
for (int i = 0; inputs[i] != NULL; i++) {
|
||||
int *list_one = calloc(strlen(input) + 1, sizeof(int *));
|
||||
int *list_two = calloc(strlen(input) + 1, sizeof(int *));
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
int i = 0;
|
||||
for (i = 0; inputs[i] != NULL; i++) {
|
||||
char **inputs_int = str_split(strdup(inputs[i]), ' ');
|
||||
list_one[i] = atoi(inputs_int[0]);
|
||||
list_two[i] = atoi(inputs_int[1]);
|
||||
}
|
||||
|
||||
list_one[i] = -2;
|
||||
list_two[i] = -2;
|
||||
|
||||
int distance = 0;
|
||||
|
||||
for (i = 0; list_one[i] != -2; i++) {
|
||||
|
||||
int min_one = INT_MAX;
|
||||
int min_two = INT_MAX;
|
||||
int min_one_index = 0;
|
||||
int min_two_index = 0;
|
||||
|
||||
for (int j = 0; list_two[j] != -2; j++) {
|
||||
if (list_one[j] != -1) {
|
||||
min_one = MIN(list_one[j], min_one);
|
||||
if (list_one[j] == min_one)
|
||||
min_one_index = j;
|
||||
}
|
||||
if (list_two[j] != -1) {
|
||||
min_two = MIN(list_two[j], min_two);
|
||||
if (list_two[j] == min_two)
|
||||
min_two_index = j;
|
||||
}
|
||||
}
|
||||
|
||||
distance += ABS(min_one - min_two);
|
||||
list_one[min_one_index] = -1;
|
||||
list_two[min_two_index] = -1;
|
||||
}
|
||||
|
||||
printf("distance: %d\n", distance);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
1000
01/main.txt
1000
01/main.txt
File diff suppressed because it is too large
Load diff
40
01/part2.c
Normal file
40
01/part2.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include "_.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
if (argc != 2)
|
||||
return 1;
|
||||
|
||||
char *input = argv[1];
|
||||
char **inputs = str_split(strdup(input), '\n');
|
||||
|
||||
int *list_one = calloc(strlen(input) + 1, sizeof(int *));
|
||||
int *list_two = calloc(strlen(input) + 1, sizeof(int *));
|
||||
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; inputs[i] != NULL; i++) {
|
||||
char **inputs_int = str_split(strdup(inputs[i]), ' ');
|
||||
list_one[i] = atoi(inputs_int[0]);
|
||||
list_two[i] = atoi(inputs_int[1]);
|
||||
}
|
||||
|
||||
list_one[i] = -2;
|
||||
list_two[i] = -2;
|
||||
|
||||
int number = 0;
|
||||
|
||||
for (i = 0; list_one[i] != -2; i++) {
|
||||
int time = 0;
|
||||
for (int j = 0; list_two[j] != -2; j++) {
|
||||
if (list_two[j] == list_one[i])
|
||||
time++;
|
||||
}
|
||||
|
||||
number += list_one[i] * time;
|
||||
}
|
||||
|
||||
printf("number: %d\n", number);
|
||||
|
||||
return 0;
|
||||
}
|
1
01/part2.sh
Executable file
1
01/part2.sh
Executable file
|
@ -0,0 +1 @@
|
|||
gcc -g -Wall -Wextra -include_.h part2.c mylib.c
|
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
7
01/test2.txt
Normal file
7
01/test2.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
24 25
|
|
@ -11,6 +11,8 @@
|
|||
#include <math.h>
|
||||
|
||||
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
|
||||
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
|
||||
#define ABS(X) ((X) >= 0 ? (X) : -(X))
|
||||
|
||||
char** str_split(char* a_str, const char a_delim);
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
gcc -g -Wall -Wextra -include_.h *.c
|
1
template/part1.sh
Normal file
1
template/part1.sh
Normal file
|
@ -0,0 +1 @@
|
|||
gcc -g -Wall -Wextra -include_.h part1.c mylib.c
|
0
template/part2.c
Normal file
0
template/part2.c
Normal file
1
template/part2.sh
Normal file
1
template/part2.sh
Normal file
|
@ -0,0 +1 @@
|
|||
gcc -g -Wall -Wextra -include_.h part2.c mylib.c
|
Loading…
Reference in a new issue