@@ -6,13 +6,13 @@ char *readLine(FILE *pFile);
6
6
int compare (const void * a , const void * b );
7
7
int findOccurrences (int value , const int array [1000 ]);
8
8
9
- const int MAX_LINE_LENGTH = 1000 ;
9
+ const int MAX_FILE_ROWS = 1000 ;
10
10
11
11
int main (void ) {
12
12
FILE * file = fopen ("data/day1.txt" , "r" );
13
13
14
- int firsts [MAX_LINE_LENGTH ];
15
- int seconds [MAX_LINE_LENGTH ];
14
+ int firsts [MAX_FILE_ROWS ];
15
+ int seconds [MAX_FILE_ROWS ];
16
16
17
17
int ctr = 0 ;
18
18
while (!feof (file )) {
@@ -27,19 +27,20 @@ int main(void) {
27
27
seconds [ctr ] = s ;
28
28
29
29
ctr ++ ;
30
+ free (line );
30
31
}
31
32
32
- qsort (firsts , MAX_LINE_LENGTH , sizeof (int ), compare );
33
- qsort (seconds , MAX_LINE_LENGTH , sizeof (int ), compare );
33
+ qsort (firsts , MAX_FILE_ROWS , sizeof (int ), compare );
34
+ qsort (seconds , MAX_FILE_ROWS , sizeof (int ), compare );
34
35
35
36
int sum = 0 ;
36
- for (int n = 0 ; n < MAX_LINE_LENGTH ; n ++ )
37
+ for (int n = 0 ; n < MAX_FILE_ROWS ; n ++ )
37
38
sum += abs (firsts [n ] - seconds [n ]);
38
39
39
40
printf ("Total distance: %d\n" , sum );
40
41
41
42
sum = 0 ;
42
- for (int n = 0 ; n < MAX_LINE_LENGTH ; n ++ ) {
43
+ for (int n = 0 ; n < MAX_FILE_ROWS ; n ++ ) {
43
44
int first = firsts [n ];
44
45
int occurrences = findOccurrences (first , seconds );
45
46
sum += first * occurrences ;
@@ -50,10 +51,10 @@ int main(void) {
50
51
return 0 ;
51
52
}
52
53
53
- int findOccurrences (int value , const int array [1000 ]) {
54
+ int findOccurrences (int value , const int array [MAX_FILE_ROWS ]) {
54
55
int count = 0 ;
55
56
if (array != NULL ) {
56
- for (int i = 0 ; i < 1000 ; i ++ ) {
57
+ for (int i = 0 ; i < MAX_FILE_ROWS ; i ++ ) {
57
58
if (array [i ] == value ) {
58
59
count ++ ;
59
60
}
0 commit comments