-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.c
113 lines (86 loc) · 2.48 KB
/
Day2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define PATH "data/day2.txt"
#define LENGTH 500
#define MAX_LENGTH 100
#define SEPERATOR " "
bool is_increasing(int arr[], size_t length) {
for (size_t i = 1; i < length; ++i) {
int previous = arr[i - 1];
int current = arr[i];
if (!(previous < current)) {
return false;
}
}
return true;
}
bool is_decreasing(int arr[], size_t length) {
for (size_t i = 1; i < length; ++i) {
int previous = arr[i - 1];
int current = arr[i];
if (!(previous > current)) {
return false;
}
}
return true;
}
bool valid_differences(int arr[], size_t length) {
for (size_t i = 1; i < length; ++i) {
int previous = arr[i - 1];
int current = arr[i];
int diff = abs(previous - current);
if (diff != 1 && diff != 2 && diff != 3) {
return false;
}
}
return true;
}
int *remove_element(int arr[], size_t length, size_t idx) {
int *new_array = calloc(length - 1, sizeof(int));
size_t it = 0;
for (size_t i = 0; i < length; ++i) {
if (i != idx) {
new_array[it++] = arr[i];
}
}
return new_array;
}
bool part2_valid(int arr[], size_t length) {
for (size_t i = 0; i < length; ++i) {
int *with_removed = remove_element(arr, length, i);
if ((is_increasing(with_removed, length - 1) || is_decreasing(with_removed, length - 1)) && valid_differences(with_removed, length - 1)) {
free(with_removed);
return true;
}
free(with_removed);
}
return false;
}
int main(int argc, char *argv[]) {
FILE *file = fopen(PATH, "r");
char text[LENGTH];
size_t part1_counter = 0;
size_t part2_counter = 0;
while (fgets(text, LENGTH - 1, file) != NULL) {
text[strcspn(text, "\n")] = '\0';
int nums[MAX_LENGTH] = { 0 };
size_t len = 0;
char *token = strtok(text, SEPERATOR);
while (token) {
nums[len] = atoi(token);
++len;
token = strtok(NULL, SEPERATOR);
}
if ((is_increasing(nums, len) || is_decreasing(nums, len)) && valid_differences(nums, len)) {
++part1_counter;
++part2_counter;
} else if (part2_valid(nums, len)) {
++part2_counter;
}
}
printf("%zu\n", part1_counter);
printf("%zu\n", part2_counter);
return 0;
}