111 lines
2.5 KiB
C
111 lines
2.5 KiB
C
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
#define LINES 1000
|
||
|
|
||
|
// power! x to the y!
|
||
|
int pow(int x, int y)
|
||
|
{
|
||
|
int ans = 1;
|
||
|
for (int i = 0; i < y; i++) ans *= x;
|
||
|
return ans;
|
||
|
}
|
||
|
// parse string into array of numbers
|
||
|
int parse_numbers(char *str_row, int *row, int n)
|
||
|
{
|
||
|
int i = 0;
|
||
|
for (; str_row[i] != '\n'; i++);
|
||
|
for (; i >= 0; i--) {
|
||
|
int pos = 0;
|
||
|
for (; str_row[i] >= '0' && str_row[i] <= '9' && i >= 0; i--) {
|
||
|
row[n] += (str_row[i] - '0') * pow(10, pos);
|
||
|
pos++;
|
||
|
}
|
||
|
n--;
|
||
|
}
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
// check if specific report is safe
|
||
|
int check_report(int *row, int len)
|
||
|
{
|
||
|
// read number. Compare next number (higher, lower, +-1-2)
|
||
|
// if you get to row[i + 1]line, ++
|
||
|
bool rising = 0;
|
||
|
bool first = 1;
|
||
|
for (int i = 0; i < len - 1; i++) {
|
||
|
// if difference isnt 1-3
|
||
|
if (abs(row[i + 1] - row[i]) > 3 || abs(row[i + 1] - row[i]) < 1)
|
||
|
return 0;
|
||
|
// only for first comparison of row. Set direction.
|
||
|
if (first && ((row[i + 1] - row[i]) < 0))
|
||
|
rising = 0;
|
||
|
else if (first)
|
||
|
rising = 1;
|
||
|
// for other comparisons, compare current direction to the previous.
|
||
|
if (!first && (rising != !((row[i + 1] - row[i]) < 0))) {
|
||
|
// printf("rising: %d while %d - %d is %d\n", rising, row[i + 1], row[i], row[i + 1] - row[i]);
|
||
|
return 0;
|
||
|
}
|
||
|
// get ready for the next comparisons!
|
||
|
first = 0;
|
||
|
}
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
int dampener(int *row, int len)
|
||
|
{
|
||
|
if (check_report(row, len))
|
||
|
return 1;
|
||
|
|
||
|
for (int i = 0; i < len; i++) {
|
||
|
int *dampened_row = malloc((len - 1) * sizeof(int));
|
||
|
// initialize shorter array, but skip element i
|
||
|
bool skipped = 0;
|
||
|
for (int j = 0; j < len - 1; j++) {
|
||
|
if (j != i && !skipped) {
|
||
|
dampened_row[j] = row[j];
|
||
|
}
|
||
|
else {
|
||
|
dampened_row[j] = row[j + 1];
|
||
|
skipped = 1;
|
||
|
}
|
||
|
}
|
||
|
if (check_report(dampened_row, len - 1)) {
|
||
|
free(dampened_row);
|
||
|
return 1;
|
||
|
}
|
||
|
free(dampened_row);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int safe = 0;
|
||
|
// read input row into array, count elements
|
||
|
FILE *input;
|
||
|
input = fopen("./input", "r");
|
||
|
char line[32];
|
||
|
for (int i = 0; i < LINES; i++) {
|
||
|
// get string (line), count numbers, put into int array (row)
|
||
|
int numbers = 1;
|
||
|
fgets(line, sizeof(line), input);
|
||
|
// count number of numbers, allocate memory
|
||
|
for (int c = 0; line[c]; c++) numbers += (line[c] == ' ');
|
||
|
int *row = (int *)calloc(numbers, sizeof(int));
|
||
|
// parse numbers into array
|
||
|
parse_numbers(line, row, numbers);
|
||
|
// for (int j = 0; j < numbers; j++) printf("%d ", row[j]);
|
||
|
// printf("\n\n");
|
||
|
|
||
|
// send elements and length to check_report, add result to sum
|
||
|
safe += dampener(row, numbers);
|
||
|
free(row);
|
||
|
}
|
||
|
|
||
|
printf("%d\n", safe);
|
||
|
}
|