aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/02/part1.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-01-27 14:02:35 +0800
committerMistivia <i@mistivia.com>2024-01-27 14:02:35 +0800
commit6580dcd9127f69aaa794472ec92bc46015dc4019 (patch)
treedc2c7e102c75180f7bd98c2f3a14f8b55f83c0f2 /advent-of-code/2023/02/part1.c
init
Diffstat (limited to 'advent-of-code/2023/02/part1.c')
-rw-r--r--advent-of-code/2023/02/part1.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/advent-of-code/2023/02/part1.c b/advent-of-code/2023/02/part1.c
new file mode 100644
index 0000000..194bf2f
--- /dev/null
+++ b/advent-of-code/2023/02/part1.c
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "str.h"
+#include "vec.h"
+
+typedef struct {
+ int r,g,b;
+} set_t;
+
+typedef struct {
+ int length;
+ set_t sets[64];
+} game_t;
+
+game_t games[100];
+
+void parse_set(const char *str, set_t *set) {
+ void* strs = str_split(str, ',');
+ set->r = 0;
+ set->g = 0;
+ set->b = 0;
+ for (int i = 0; i < vec_size(strs); i++) {
+ char *str = vec_get(strs, i);
+ void* infos = str_split(str, ' ');
+ if (strcmp(vec_get(infos, 1), "red") == 0) {
+ set->r = strtol(vec_get(infos, 0), NULL, 10);
+ } else if (strcmp(vec_get(infos, 1), "blue") == 0) {
+ set->b = strtol(vec_get(infos, 0), NULL, 10);
+ } else if (strcmp(vec_get(infos, 1), "green") == 0) {
+ set->g = strtol(vec_get(infos, 0), NULL, 10);
+ }
+ }
+}
+
+void parse_game(const char *line, game_t *game) {
+ void *strs = str_split(line, ':');
+ void *sets_str = str_split(vec_get(strs, 1), ';');
+ for (int i = 0; i < vec_size(sets_str); i++) {
+ set_t s;
+ parse_set(str_strip(vec_get(sets_str, i)), &s);
+ game->sets[i] = s;
+ }
+ game->length = vec_size(sets_str);
+}
+
+int is_possible(game_t *game) {
+ for (int i = 0; i < game->length; i++) {
+ if (game->sets[i].r <= 12
+ && game->sets[i].g <= 13
+ && game->sets[i].b <= 14) {
+ continue;
+ }
+ return 0;
+ }
+ return 1;
+}
+
+int main() {
+ FILE *fp = fopen("./input", "r");
+ int sum = 0;
+ for (int i = 0; i < 100; i++) {
+ char *line = fgetline(fp);
+ parse_game(line, &games[i]);
+ }
+ for (int i = 0; i < 100; i++) {
+ if (is_possible(&games[i])) {
+ sum += i + 1;
+ }
+ }
+ printf("%d\n", sum);
+ return 0;
+}