65 lines
1.5 KiB
C
65 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <string.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) {
|
|
size_t sz = 0;
|
|
char **lines = get_lines("input/2023/02", &sz);
|
|
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);
|
|
}
|