feat: day1_1
This commit is contained in:
parent
1e29d0707a
commit
04e8802dac
2 changed files with 82 additions and 3 deletions
|
|
@ -1,5 +1,84 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main(void) {
|
#include <stdlib.h>
|
||||||
printf("TEST");
|
#include <string.h>
|
||||||
return 0;
|
|
||||||
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
|
int count_lines(char *filename);
|
||||||
|
int compare_ints(const void *a, const void *b);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
FILE *file;
|
||||||
|
char filename[] = "input.txt";
|
||||||
|
char *buffer = malloc(BUFFER_SIZE);
|
||||||
|
char *saveptr;
|
||||||
|
char *current_number_string;
|
||||||
|
const int lines = count_lines(filename);
|
||||||
|
int *first_numbers = malloc(lines * sizeof(int));
|
||||||
|
int *second_numbers = malloc(lines * sizeof(int));
|
||||||
|
int current_position = 0;
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
file = fopen(filename, "r");
|
||||||
|
|
||||||
|
if (file == NULL) {
|
||||||
|
perror("Error opening file");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fgets(buffer, BUFFER_SIZE, file) != NULL) {
|
||||||
|
char *line = buffer;
|
||||||
|
int is_first = 1;
|
||||||
|
while ((current_number_string = strtok_r(line, " ", &saveptr))) {
|
||||||
|
if (current_number_string[strlen(current_number_string) - 1] == '\n') {
|
||||||
|
current_number_string[strlen(current_number_string) - 1] = '\0';
|
||||||
|
}
|
||||||
|
if (is_first) {
|
||||||
|
first_numbers[current_position] = atoi(current_number_string);
|
||||||
|
} else {
|
||||||
|
second_numbers[current_position] = atoi(current_number_string);
|
||||||
|
}
|
||||||
|
is_first ^= 1;
|
||||||
|
if (line) {
|
||||||
|
line = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
current_position++;
|
||||||
|
}
|
||||||
|
qsort(first_numbers, lines, sizeof(int), compare_ints);
|
||||||
|
qsort(second_numbers, lines, sizeof(int), compare_ints);
|
||||||
|
|
||||||
|
for (int i = 0; i < lines; i++) {
|
||||||
|
int difference = abs(first_numbers[i] - second_numbers[i]);
|
||||||
|
sum += difference;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%d\n", sum);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int count_lines(char *filename) {
|
||||||
|
char c;
|
||||||
|
int lines = 0;
|
||||||
|
FILE *file = fopen(filename, "r");
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!feof(file)) {
|
||||||
|
c = fgetc(file);
|
||||||
|
if (c == '\n') {
|
||||||
|
lines++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
int compare_ints(const void *a, const void *b) {
|
||||||
|
return (*(int *)a - *(int *)b);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
2024/Day1/test
Executable file
BIN
2024/Day1/test
Executable file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue