refactor part 1 to not have any warnings
This commit is contained in:
parent
a7c15c246b
commit
29d21867c1
1 changed files with 9 additions and 9 deletions
16
day_2/1.c
16
day_2/1.c
|
@ -3,10 +3,12 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
typedef struct levels {
|
typedef struct levels {
|
||||||
size_t mem_size;
|
size_t mem_size;
|
||||||
size_t size;
|
size_t size;
|
||||||
unsigned int *data;
|
int *data;
|
||||||
} levels_t;
|
} levels_t;
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
@ -17,8 +19,7 @@ int main(void) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *line = NULL;
|
char buffer[BUFFER_SIZE];
|
||||||
size_t size;
|
|
||||||
|
|
||||||
levels_t levels;
|
levels_t levels;
|
||||||
levels.mem_size = 10;
|
levels.mem_size = 10;
|
||||||
|
@ -26,8 +27,8 @@ int main(void) {
|
||||||
levels.data = malloc(sizeof(unsigned int) * levels.mem_size);
|
levels.data = malloc(sizeof(unsigned int) * levels.mem_size);
|
||||||
|
|
||||||
unsigned int sum = 0;
|
unsigned int sum = 0;
|
||||||
while (getline(&line, &size, fp) > 0) {
|
while (fgets(buffer, BUFFER_SIZE, fp) != NULL) {
|
||||||
char *next = strtok(line, " ");
|
char *next = strtok(buffer, " ");
|
||||||
|
|
||||||
while (next != NULL) {
|
while (next != NULL) {
|
||||||
if (levels.size >= levels.mem_size) {
|
if (levels.size >= levels.mem_size) {
|
||||||
|
@ -36,7 +37,7 @@ int main(void) {
|
||||||
realloc(levels.data, levels.mem_size * sizeof(unsigned int));
|
realloc(levels.data, levels.mem_size * sizeof(unsigned int));
|
||||||
}
|
}
|
||||||
|
|
||||||
sscanf(next, "%u", &levels.data[levels.size]);
|
sscanf(next, "%d", &levels.data[levels.size]);
|
||||||
levels.size++;
|
levels.size++;
|
||||||
|
|
||||||
next = strtok(NULL, " ");
|
next = strtok(NULL, " ");
|
||||||
|
@ -45,7 +46,7 @@ int main(void) {
|
||||||
bool safe = true;
|
bool safe = true;
|
||||||
int diff = levels.data[1] - levels.data[0];
|
int diff = levels.data[1] - levels.data[0];
|
||||||
int increasing = diff > 0 ? 1 : -1;
|
int increasing = diff > 0 ? 1 : -1;
|
||||||
for (int i = 1; i < levels.size; i++) {
|
for (size_t i = 1; i < levels.size; i++) {
|
||||||
diff = increasing * (levels.data[i] - levels.data[i - 1]);
|
diff = increasing * (levels.data[i] - levels.data[i - 1]);
|
||||||
|
|
||||||
if (diff < 1 || diff > 3) {
|
if (diff < 1 || diff > 3) {
|
||||||
|
@ -61,7 +62,6 @@ int main(void) {
|
||||||
levels.size = 0;
|
levels.size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(line);
|
|
||||||
free(levels.data);
|
free(levels.data);
|
||||||
|
|
||||||
printf("%u\n", sum);
|
printf("%u\n", sum);
|
||||||
|
|
Loading…
Reference in a new issue