c 2024 3 part 2

This commit is contained in:
Bill Rossi 2024-12-03 02:57:47 -05:00
parent 99f7f21611
commit d7fc4fb192

View File

@ -11,58 +11,63 @@ int main() {
char *line; char *line;
int sum = 0; int sum = 0;
int cond_sum = 0;
char d1[10], d2[10]; char d1[10], d2[10];
int d1_index, d2_index; int d1_index, d2_index;
while ((line = aoc_read_line()) != NULL) { enum State state = M;
enum State state = M; bool do_mult = true;
char *c = line; char *c = aoc_read_input();
while(*c != '\0') {
switch (state) { while(*c != '\0') {
case M: if (memcmp(c, "do()", 4) == 0) do_mult = true;
if (*c == 'm') state = U; else if (memcmp(c, "don't()", 7) == 0) do_mult = false;
break;
case U: switch (state) {
if (*c == 'u') state = L; case M:
else state = M; if (*c == 'm') state = U;
break; break;
case L: case U:
if (*c == 'l') state = OPEN; if (*c == 'u') state = L;
else state = M; else state = M;
break; break;
case OPEN: case L:
d1_index = 0; if (*c == 'l') state = OPEN;
d2_index = 0; else state = M;
if (*c == '(') state = D1; break;
else state = M; case OPEN:
break; d1_index = 0;
case D1: d2_index = 0;
if (*c >= '0' && *c <= '9') { if (*c == '(') state = D1;
d1[d1_index++] = *c; else state = M;
} break;
else if(*c == ',') state = D2; case D1:
else state = M; if (*c >= '0' && *c <= '9') {
break; d1[d1_index++] = *c;
case D2:
if (*c >= '0' && *c <= '9') {
d2[d2_index++] = *c;
}
else if(*c == ')') {
d1[d1_index] = '\0';
d2[d2_index] = '\0';
sum += atoi(d1) * atoi(d2);
state = M;
}
else state = M;
break;
} }
c++; else if(*c == ',') state = D2;
else state = M;
break;
case D2:
if (*c >= '0' && *c <= '9') {
d2[d2_index++] = *c;
}
else if(*c == ')') {
d1[d1_index] = '\0';
d2[d2_index] = '\0';
sum += atoi(d1) * atoi(d2);
if (do_mult) cond_sum += atoi(d1) * atoi(d2);
state = M;
}
else state = M;
break;
} }
c++;
} }
printf("Part 1: %d\n", sum); printf("Part 1: %d\n", sum);
// printf("Part 2: %d\n", safe_skip_report_count); printf("Part 2: %d\n", cond_sum);
aoc_free(); aoc_free();
return 0; return 0;