aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/04/part1.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-02-15 17:49:56 +0800
committerMistivia <i@mistivia.com>2024-02-15 17:51:00 +0800
commitf163146fe4b31f1b9e4e25f7f61a5c6928e2fe5b (patch)
treee3e99fc5eb674c783ac2947a4276f1b92a892a64 /advent-of-code/2023/04/part1.c
parent58709a89622062b527b242f65aa0501a4efd47d3 (diff)
refactor advent of code 2023 day 04 from c to racket
Diffstat (limited to 'advent-of-code/2023/04/part1.c')
-rw-r--r--advent-of-code/2023/04/part1.c69
1 files changed, 0 insertions, 69 deletions
diff --git a/advent-of-code/2023/04/part1.c b/advent-of-code/2023/04/part1.c
deleted file mode 100644
index 4dd94ba..0000000
--- a/advent-of-code/2023/04/part1.c
+++ /dev/null
@@ -1,69 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <math.h>
-
-#include "vec.h"
-#include "str.h"
-
-typedef struct {
- void *win_nums;
- void *nums;
-} card_t;
-
-void *parse_input() {
- FILE *fp = fopen("./input", "r");
- void *cards = new_vec();
- char *line;
- while ((line = fgetline(fp)) != NULL) {
- line = str_strip(line);
- line = vec_get(str_split(line, ':'), 1);
- line = str_strip(line);
- void *splited = str_split(line, '|');
- char *win_str = str_strip(vec_get(splited, 0));
- char *num_str = str_strip(vec_get(splited, 1));
- void *winnums_str = str_split(win_str, ' ');
- void *nums_str = str_split(num_str, ' ');
-
- card_t *card = malloc(sizeof(card_t));
- card->win_nums = new_vec();
- card->nums = new_vec();
- for (int i = 0; i < vec_size(winnums_str); i++) {
- int *n = malloc(sizeof(int));
- *n = strtol(vec_get(winnums_str, i), NULL, 10);
- vec_push_back(card->win_nums, n);
- }
- for (int i = 0; i < vec_size(nums_str); i++) {
- int *n = malloc(sizeof(int));
- *n = strtol(vec_get(nums_str, i), NULL, 10);
- vec_push_back(card->nums, n);
- }
- vec_push_back(cards, card);
- }
- return cards;
-}
-
-int points(card_t *card) {
- int win_count = 0;
- for (int i = 0; i < vec_size(card->nums); i++) {
- int num = *(int*)vec_get(card->nums, i);
- for (int j = 0; j < vec_size(card->win_nums); j++) {
- if (num == *(int*)vec_get(card->win_nums, j)) {
- win_count++;
- break;
- }
- }
- }
- if (win_count == 0) return 0;
- return (int)pow(2, win_count - 1);
-}
-
-int main() {
- void *cards = parse_input();
- int sum = 0;
- for (int i = 0; i < vec_size(cards); i++) {
- int p = points(vec_get(cards, i));
- sum += p;
- }
- printf("%d\n", sum);
- return 0;
-}