c 2024 3 part 1
This commit is contained in:
parent
0d9abc42ce
commit
99f7f21611
69
c/2024/3/mull_it_over.c
Normal file
69
c/2024/3/mull_it_over.c
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include "../../lib/aoc.h"
|
||||||
|
|
||||||
|
enum State { M, U, L, OPEN, D1, COMMA, D2, CLOSE };
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char *line;
|
||||||
|
|
||||||
|
int 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;
|
||||||
|
}
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Part 1: %d\n", sum);
|
||||||
|
// printf("Part 2: %d\n", safe_skip_report_count);
|
||||||
|
|
||||||
|
aoc_free();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user