This commit is contained in:
Evan Burkey 2023-12-05 21:59:01 -08:00
parent 3238b8b402
commit 31129c5916
3 changed files with 63 additions and 5 deletions

View File

@ -5,6 +5,9 @@
#define DEFAULT_DELIM " \t\n\r\f\v" #define DEFAULT_DELIM " \t\n\r\f\v"
#define MAX(x, y) (x) > (y) ? (x) : (y)
#define MIN(x, y) (x) < (y) ? (x) : (y)
char *md5_str(const char *); char *md5_str(const char *);
char *capture_system(const char *); char *capture_system(const char *);

View File

@ -1,10 +1,64 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <string.h>
#include "lfinput.h" #include "lfinput.h"
#include "advent_utility.h"
struct Game {
int id;
int red_max;
int green_max;
int blue_max;
};
static void parse(char *input, struct Game *g) {
memset(g, 0, sizeof(struct Game));
sscanf(input, "Game %d:", &g->id);
const char *c = strchr(input, ':') + 1;
char cpy[1024];
strcpy(cpy, c);
char *token = strtok(cpy, ",;");
while (token != NULL) {
int count;
char color[10];
if (sscanf(token, "%d %9s", &count, color) == 2) {
if (strcmp(color, "red") == 0) {
g->red_max = MAX(count, g->red_max);
} else if (strcmp(color, "blue") == 0) {
g->blue_max = MAX(count, g->blue_max);
} else if (strcmp(color, "green") == 0) {
g->green_max = MAX(count, g->green_max);
}
}
token = strtok(NULL, ",;");
}
}
#define RED 12
#define GREEN 13
#define BLUE 14
static int valid(struct Game *g) {
return g->green_max <= GREEN && g->red_max <= RED && g->blue_max <= BLUE;
}
void advent2023day02(void) { void advent2023day02(void) {
char *input = get_input("input/2023/02"); size_t sz = 0;
printf("Solution for Day 02 of 2023 is not completed yet\n"); char **lines = get_lines("input/2023/02", &sz);
free(input); int p1 = 0, p2 = 0;
for (size_t i = 0; i < sz; ++i) {
struct Game g;
parse(lines[i], &g);
if (valid(&g)) {
p1 += g.id;
}
p2 += g.red_max * g.blue_max * g.green_max;
}
printf("%d\n%d\n", p1, p2);
del_lines(lines);
} }

View File

@ -61,3 +61,4 @@ Vector *string_to_int_vector(const char *input_string, const char* delim) {
free(copy); free(copy);
return v; return v;
} }