Dynamically count lines
This commit is contained in:
		
							parent
							
								
									b8a1300703
								
							
						
					
					
						commit
						3c366ab7d1
					
				| @ -4,14 +4,12 @@ | |||||||
| 
 | 
 | ||||||
| #include "../../lib/aoc.h" | #include "../../lib/aoc.h" | ||||||
| 
 | 
 | ||||||
| // TODO figure this out dynamically
 |  | ||||||
| #define ARRAY_SIZE 1000 |  | ||||||
| 
 |  | ||||||
| int main() { | int main() { | ||||||
|   char *line; |   char *line; | ||||||
|  |   int line_count = aoc_line_count(); | ||||||
| 
 | 
 | ||||||
|   int left[ARRAY_SIZE]; |   int left[line_count]; | ||||||
|   int right[ARRAY_SIZE]; |   int right[line_count]; | ||||||
|   int index = 0; |   int index = 0; | ||||||
| 
 | 
 | ||||||
|   while ((line = aoc_read_line()) != NULL) { |   while ((line = aoc_read_line()) != NULL) { | ||||||
| @ -22,12 +20,12 @@ int main() { | |||||||
|     index++; |     index++; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   qsort(left, ARRAY_SIZE, sizeof(int), aoc_sort_int); |   qsort(left, line_count, sizeof(int), aoc_sort_int); | ||||||
|   qsort(right, ARRAY_SIZE, sizeof(int), aoc_sort_int); |   qsort(right, line_count, sizeof(int), aoc_sort_int); | ||||||
| 
 | 
 | ||||||
|   int sum = 0; |   int sum = 0; | ||||||
| 
 | 
 | ||||||
|   for (int i = 0; i < ARRAY_SIZE; i++) { |   for (int i = 0; i < line_count; i++) { | ||||||
|     sum += abs(left[i] - right[i]); |     sum += abs(left[i] - right[i]); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -36,9 +34,9 @@ int main() { | |||||||
|   int similarity_score = 0; |   int similarity_score = 0; | ||||||
|   int j = 0; |   int j = 0; | ||||||
| 
 | 
 | ||||||
|   for (int i = 0; i < ARRAY_SIZE; i++) { |   for (int i = 0; i < line_count; i++) { | ||||||
|     int right_count = 0; |     int right_count = 0; | ||||||
|     for (; j < ARRAY_SIZE; j++) { |     for (; j < line_count; j++) { | ||||||
|       if (left[i] == right[j]) right_count++; |       if (left[i] == right[j]) right_count++; | ||||||
|       else if (left[i] < right[j]) break; |       else if (left[i] < right[j]) break; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -54,6 +54,15 @@ void aoc_free(void) { | |||||||
|   free(read_line_buffer); |   free(read_line_buffer); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int aoc_line_count(void) { | ||||||
|  |   if (input_buffer == NULL) aoc_read_input(); | ||||||
|  | 
 | ||||||
|  |   int count = 0; | ||||||
|  |   for (int i = 0; i < strlen(input_buffer); i++) { | ||||||
|  |     if (input_buffer[i] == '\n') count++; | ||||||
|  |   } | ||||||
|  |   return count; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| int aoc_sort_int(const void *a, const void *b) { | int aoc_sort_int(const void *a, const void *b) { | ||||||
|   return *(int*) a - *(int*) b; |   return *(int*) a - *(int*) b; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user