-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
146 lines (113 loc) · 3.12 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 12
const char* input_path = "./input.txt";
int is_number(char c) {
return c > 47 && c < 58;
}
void shift(char* buffer, const int buffer_size) {
for (int i = 1; i < buffer_size; i++) {
buffer[i - 1] = buffer[i];
}
buffer[buffer_size - 1] = '\0';
}
void init(FILE* file, char* buffer, const int buffer_size) {
for (int i = 1; i < buffer_size; i++) {
buffer[i] = fgetc(file);
}
}
int advance_window(FILE* file, char* buffer, const int buffer_size) {
shift(buffer, buffer_size);
buffer[buffer_size - 1] = fgetc(file);
char end[buffer_size];
memset(end, -1, buffer_size * sizeof(char));
if (buffer[buffer_size - 1] == EOF || buffer[buffer_size - 1]< 32) {
if (memcmp(buffer, end, buffer_size * sizeof(char)) == 0) {
return 0;
}
return 1;
} else {
return 1;
}
}
int starts_with(char* text, char* start, const int length) {
return memcmp(text, start, length * sizeof(char)) == 0;
}
int try_get_numbers(char* text, int* first, int* second) {
const int first_start = 4;
const int max_length = 3;
int second_start = 0;
char first_buffer[4] = {0};
char second_buffer[4] = {0};
int first_end;
for (int i = first_start; i < first_start + max_length + 1; i++) {
first_end = i;
if (i == first_start + max_length && text[i] != ',') {
return 0;
}
if (is_number(text[i])) {
first_buffer[i - first_start] = text[i];
continue;
}
if (first_end <= first_start) {
return 0;
}
break;
}
if (text[first_end] != ',') {
return 0;
}
second_start = first_end + 1;
for (int i = second_start; i < second_start + max_length + 1; i++) {
if (i == second_start + max_length && text[i] != ')') {
return 0;
}
if (is_number(text[i])) {
second_buffer[i - second_start] = text[i];
continue;
}
if (text[i] == ')' && i > second_start) {
break;
}
return 0;
}
*first = atoi(first_buffer);
*second = atoi(second_buffer);
return 1;
}
int main() {
FILE* file = fopen(input_path, "r");
if (file == NULL) {
fprintf(stderr, "Failed to open the file");
return 1;
}
char buffer[BUFFER_SIZE];
int result = 0;
int first, second;
init(file, buffer, BUFFER_SIZE);
int enabled = 1;
while(advance_window(file, buffer, BUFFER_SIZE)) {
if (starts_with(buffer, "do()", 4)) {
enabled = 1;
continue;
}
if (starts_with(buffer, "don't()", 7)) {
enabled = 0;
continue;
}
if (enabled == 0) {
continue;
}
if (!starts_with(buffer, "mul(", 4)) {
continue;
}
if (!try_get_numbers(buffer, &first, &second)) {
continue;
}
result += first * second;
}
fclose(file);
printf("Result: %d\n", result);
return 0;
}