Explorar o código

refactor advent of code 2023 day 02 from c to racket

Mistivia hai 1 ano
pai
achega
2cedcf3dfd

+ 0 - 0
advent-of-code/2023/01/part1.rkt → advent-of-code/2023/01/1.rkt


+ 0 - 0
advent-of-code/2023/01/part2.rkt → advent-of-code/2023/01/2.rkt


+ 63 - 0
advent-of-code/2023/02/1.rkt

@@ -0,0 +1,63 @@
+#lang racket
+
+(define fp (open-input-file "input"))
+
+(define (get-lines fp)
+  (let loop ((lines '()))
+    (define l (read-line fp))
+    (if (eof-object? l)
+      (reverse lines)
+      (loop (cons l lines)))))
+
+(define lines (get-lines fp))
+
+(define (extract-game line)
+  (define game-str (cadr (string-split line ":")))
+  (define sets-str (string-split game-str ";"))
+  (set! sets-str (map string-trim sets-str))
+  (map extract-set sets-str))
+
+(define (extract-set str)
+  (define r 0)
+  (define g 0)
+  (define b 0)
+  (define balls (string-split str ","))
+  (set! balls
+    (map (lambda (s)
+           (define pair (map string-trim (string-split s " ")))
+           (list (string->number (car pair)) (cadr pair)))
+         balls))
+  (let loop ((balls balls))
+    (if (null? balls)
+      '()
+      (let ()
+        (define ball (car balls))
+        (define number (car ball))
+        (define color (cadr ball))
+        (cond ((string=? "red" color) (set! r number))
+              ((string=? "green" color) (set! g number))
+              ((string=? "blue" color) (set! b number)))
+        (loop (cdr balls)))))
+  (list r g b))
+
+(define (possible? game)
+  (if (null? game)
+    #t
+    (let ()
+      (define head (car game))
+      (if (or (> (car head) 12)    ; r
+              (> (cadr head) 13)   ; g
+              (> (caddr head) 14)) ; b
+        #f
+        (possible? (cdr game))))))
+
+(define (enumerate lst)
+  (let loop ((i 1) (ret '()) (remain lst))
+    (if (null? remain)
+      (reverse ret)
+      (loop (+ 1 i) (cons (list (car remain) i) ret) (cdr remain)))))
+
+(apply + (map cadr
+              (filter (lambda (game)
+                        (possible? (car game)))
+                      (enumerate (map extract-game lines)))))

+ 49 - 0
advent-of-code/2023/02/2.rkt

@@ -0,0 +1,49 @@
+#lang racket
+
+(define fp (open-input-file "input"))
+
+(define (get-lines fp)
+  (let loop ((lines '()))
+    (define l (read-line fp))
+    (if (eof-object? l)
+      (reverse lines)
+      (loop (cons l lines)))))
+
+(define lines (get-lines fp))
+
+(define (extract-game line)
+  (define game-str (cadr (string-split line ":")))
+  (define sets-str (string-split game-str ";"))
+  (set! sets-str (map string-trim sets-str))
+  (map extract-set sets-str))
+
+(define (extract-set str)
+  (define r 0)
+  (define g 0)
+  (define b 0)
+  (define balls (string-split str ","))
+  (set! balls
+    (map (lambda (s)
+           (define pair (map string-trim (string-split s " ")))
+           (list (string->number (car pair)) (cadr pair)))
+         balls))
+  (let loop ((balls balls))
+    (if (null? balls)
+      '()
+      (let ()
+        (define ball (car balls))
+        (define number (car ball))
+        (define color (cadr ball))
+        (cond ((string=? "red" color) (set! r number))
+              ((string=? "green" color) (set! g number))
+              ((string=? "blue" color) (set! b number)))
+        (loop (cdr balls)))))
+  (list r g b))
+
+(define (power game)
+  (define rs (map car game))
+  (define gs (map cadr game))
+  (define bs (map caddr game))
+  (* (apply max rs) (apply max gs) (apply max bs)))
+
+(apply + (map power (map extract-game lines)))

+ 0 - 14
advent-of-code/2023/02/Makefile

@@ -1,14 +0,0 @@
-all: run
-
-run: part1 part2
-	./part1
-	./part2
-
-part1: part1.c
-	gcc -g -I../lib/ ../lib/*.c part1.c -o part1
-
-part2: part2.c
-	gcc -g -I../lib/ ../lib/*.c part2.c -o part2
-
-clean:
-	rm part1 part2

+ 0 - 75
advent-of-code/2023/02/part1.c

@@ -1,75 +0,0 @@
-#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;
-}

+ 0 - 73
advent-of-code/2023/02/part2.c

@@ -1,73 +0,0 @@
-#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 power(game_t *game) {
-    int maxr = 0;
-    int maxg = 0;
-    int maxb = 0;
-    for (int i = 0; i < game->length; i++) {
-        if (game->sets[i].r > maxr) maxr = game->sets[i].r;
-        if (game->sets[i].g > maxg) maxg = game->sets[i].g;
-        if (game->sets[i].b > maxb) maxb = game->sets[i].b;
-    }
-    return maxr * maxg * maxb;
-}
-
-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++) {
-        sum += power(&games[i]);
-    }
-    printf("%d\n", sum);    
-    return 0;
-}