2024-12-08 14:37:51 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2024-12-08 17:15:12 -07:00
|
|
|
#define BUFFER_SIZE 1024
|
|
|
|
|
2024-12-08 14:37:51 -07:00
|
|
|
typedef struct levels {
|
|
|
|
size_t mem_size;
|
|
|
|
size_t size;
|
2024-12-08 17:15:12 -07:00
|
|
|
int *data;
|
2024-12-08 14:37:51 -07:00
|
|
|
} levels_t;
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
FILE *fp = fopen("input", "r");
|
|
|
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
perror("input");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-12-08 17:15:12 -07:00
|
|
|
char buffer[BUFFER_SIZE];
|
2024-12-08 14:37:51 -07:00
|
|
|
|
|
|
|
levels_t levels;
|
|
|
|
levels.mem_size = 10;
|
|
|
|
levels.size = 0;
|
|
|
|
levels.data = malloc(sizeof(unsigned int) * levels.mem_size);
|
|
|
|
|
|
|
|
unsigned int sum = 0;
|
2024-12-08 17:15:12 -07:00
|
|
|
while (fgets(buffer, BUFFER_SIZE, fp) != NULL) {
|
|
|
|
char *next = strtok(buffer, " ");
|
2024-12-08 14:37:51 -07:00
|
|
|
|
|
|
|
while (next != NULL) {
|
|
|
|
if (levels.size >= levels.mem_size) {
|
|
|
|
levels.mem_size *= 2;
|
|
|
|
levels.data =
|
|
|
|
realloc(levels.data, levels.mem_size * sizeof(unsigned int));
|
|
|
|
}
|
|
|
|
|
2024-12-08 17:15:12 -07:00
|
|
|
sscanf(next, "%d", &levels.data[levels.size]);
|
2024-12-08 14:37:51 -07:00
|
|
|
levels.size++;
|
|
|
|
|
|
|
|
next = strtok(NULL, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool safe = true;
|
|
|
|
int diff = levels.data[1] - levels.data[0];
|
|
|
|
int increasing = diff > 0 ? 1 : -1;
|
2024-12-08 17:15:12 -07:00
|
|
|
for (size_t i = 1; i < levels.size; i++) {
|
2024-12-08 14:37:51 -07:00
|
|
|
diff = increasing * (levels.data[i] - levels.data[i - 1]);
|
|
|
|
|
|
|
|
if (diff < 1 || diff > 3) {
|
|
|
|
safe = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (safe) {
|
|
|
|
sum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
levels.size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(levels.data);
|
|
|
|
|
|
|
|
printf("%u\n", sum);
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
2024-12-08 17:15:12 -07:00
|
|
|
}
|