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