-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18b.c
274 lines (254 loc) · 8.3 KB
/
day18b.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "common.h"
#include "stringview.h"
enum Operator {
OP_ADD,
OP_MULT
};
enum ExpressionType {
EXPR_LITERAL,
EXPR_COMPOUND
};
struct ExpressionCompound {
struct Expression * first;
struct Expression * second;
enum Operator op;
};
struct Expression {
enum ExpressionType type;
union {
long int value;
struct ExpressionCompound compound;
};
};
long int evaluateExpression (struct Expression const * expr) {
ASSERT(expr);
long int result = 0;
if (expr->type == EXPR_LITERAL) {
return expr->value;
} else {
long int firstval = evaluateExpression(expr->compound.first);
long int secondval = evaluateExpression(expr->compound.second);
switch (expr->compound.op) {
case OP_ADD: {
result = firstval + secondval;
} break;
case OP_MULT: {
result = firstval * secondval;
} break;
default: {
ASSERT(0);
}
}
}
return result;
}
enum TokenType {
TOKEN_NONE,
TOKEN_INTEGER,
TOKEN_ADD,
TOKEN_MULT,
TOKEN_OPEN_PAREN,
TOKEN_CLOSE_PAREN
};
struct Token {
enum TokenType type;
long int value;
};
struct Expression * newLiteralExpression (long int value) {
struct Expression * expr = malloc(sizeof(struct Expression));
expr->type = EXPR_LITERAL;
expr->value = value;
return expr;
}
struct Expression * newCompoundExpression (struct Expression * first, struct Expression * second, enum Operator op) {
struct Expression * expr = malloc(sizeof(struct Expression));
expr->type = EXPR_COMPOUND;
expr->compound.first = first;
expr->compound.second = second;
expr->compound.op = op;
return expr;
}
void freeExpression (struct Expression * expr) {
if (expr->type == EXPR_COMPOUND) {
freeExpression(expr->compound.first);
freeExpression(expr->compound.second);
free(expr->compound.first);
free(expr->compound.second);
expr->compound.first = NULL;
expr->compound.second = NULL;
}
}
int precedence (enum TokenType type) {
switch (type) {
case TOKEN_ADD: {
return 2;
}
case TOKEN_MULT: {
return 1;
}
default: {
ASSERT(0);
}
}
}
struct Expression * parseExpression (struct Token * start, struct Token * end) {
size_t operator_stack_count = 0;
struct Token operator_stack [128];
struct Token * op = start;
for (struct Token * it = start; it != end; ++it) {
struct Token t = *it;
switch (t.type) {
case TOKEN_INTEGER: {
ASSERT(op != end);
*(op++) = t;
} break;
case TOKEN_ADD:
case TOKEN_MULT: {
while (operator_stack_count && operator_stack[operator_stack_count-1].type != TOKEN_OPEN_PAREN && precedence(operator_stack[operator_stack_count-1].type) >= precedence(t.type)) {
ASSERT(op != end);
*(op++) = operator_stack[--operator_stack_count];
}
ASSERT(operator_stack_count < ARRAYCOUNT(operator_stack));
operator_stack[operator_stack_count++] = t;
} break;
case TOKEN_OPEN_PAREN: {
ASSERT(operator_stack_count < ARRAYCOUNT(operator_stack));
operator_stack[operator_stack_count++] = t;
} break;
case TOKEN_CLOSE_PAREN: {
ASSERT(operator_stack_count);
while (operator_stack[operator_stack_count-1].type != TOKEN_OPEN_PAREN) {
ASSERT(op != end);
*(op++) = operator_stack[--operator_stack_count];
ASSERT(operator_stack_count);
}
if (operator_stack[operator_stack_count-1].type == TOKEN_OPEN_PAREN) {
operator_stack_count -= 1;
}
} break;
default: {
ASSERT(0);
}
}
}
while (operator_stack_count) {
ASSERT(operator_stack[operator_stack_count-1].type != TOKEN_OPEN_PAREN);
ASSERT(op != end);
*(op++) = operator_stack[--operator_stack_count];
}
end = op;
#if 0
for (struct Token * it = start; it != end; ++it) {
switch (it->type) {
case TOKEN_INTEGER: {
if (it != start) {
putchar(' ');
}
printf("%li", it->value);
} break;
case TOKEN_ADD: {
fputs(" +", stdout);
} break;
case TOKEN_MULT: {
fputs(" *", stdout);
} break;
default: {
ASSERT(0);
}
}
}
printf("\n");
#endif
size_t expr_stack_count = 0;
struct Expression * expr_stack [128];
for (struct Token * it = start; it != end; ++it) {
switch (it->type) {
case TOKEN_INTEGER: {
ASSERT(expr_stack_count < ARRAYCOUNT(expr_stack));
expr_stack[expr_stack_count++] = newLiteralExpression(it->value);
} break;
case TOKEN_ADD: {
ASSERT(expr_stack_count >= 2);
struct Expression * second = expr_stack[expr_stack_count-1];
struct Expression * first = expr_stack[expr_stack_count-2];
expr_stack[expr_stack_count-2] = newCompoundExpression(first, second, OP_ADD);
expr_stack_count = expr_stack_count-1;
} break;
case TOKEN_MULT: {
ASSERT(expr_stack_count >= 2);
struct Expression * second = expr_stack[expr_stack_count-1];
struct Expression * first = expr_stack[expr_stack_count-2];
expr_stack[expr_stack_count-2] = newCompoundExpression(first, second, OP_MULT);
expr_stack_count = expr_stack_count-1;
} break;
default: {
ASSERT(0);
}
}
}
ASSERT(expr_stack_count == 1);
return expr_stack[0];
}
int main (int argc, char ** argv) {
long int sum = 0;
do {
char buf [BUFSIZ];
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
if (!s) {
break;
}
size_t tokens_capacity = 1024;
size_t tokens_count = 0;
struct Token tokens [tokens_capacity];
StringView_t line = sv_view_c_string(s);
sv_eat_spaces(&line);
while (!sv_is_empty(&line)) {
char c = *line.start; // TODO: sv_peek
if (isdigit(c)) {
struct Token token = {
.type = TOKEN_INTEGER,
.value = strtol(line.start, (char **)&line.start, 10)
};
ASSERT(tokens_count < tokens_capacity);
tokens[tokens_count++] = token;
} else if (c == '+') {
sv_eat_char(&line);
struct Token token = {
.type = TOKEN_ADD
};
ASSERT(tokens_count < tokens_capacity);
tokens[tokens_count++] = token;
} else if (c == '*') {
sv_eat_char(&line);
struct Token token = {
.type = TOKEN_MULT
};
ASSERT(tokens_count < tokens_capacity);
tokens[tokens_count++] = token;
} else if (c == '(') {
sv_eat_char(&line);
struct Token token = {
.type = TOKEN_OPEN_PAREN
};
ASSERT(tokens_count < tokens_capacity);
tokens[tokens_count++] = token;
} else if (c == ')') {
sv_eat_char(&line);
struct Token token = {
.type = TOKEN_CLOSE_PAREN
};
ASSERT(tokens_count < tokens_capacity);
tokens[tokens_count++] = token;
} else {
ERROR("Invalid token");
}
sv_eat_spaces(&line);
}
struct Expression * expr = parseExpression(&(tokens[0]), &(tokens[tokens_count]));
long int result = evaluateExpression(expr);
//printf("%li\n", result);
sum += result;
freeExpression(expr);
} while (1);
DISP(sum);
}