Mistivia 1 year ago
parent
commit
a19a1b970e

+ 2 - 8
advent-of-code/2023/01/1.rkt

@@ -1,14 +1,8 @@
 #lang racket
 
-(define fp (open-input-file "input"))
-(define sum 0)
+(require "../lib/utils.rkt")
 
-(define (get-lines fp)
-  (let loop ((lines '()))
-    (define l (read-line fp))
-    (if (eof-object? l)
-      (reverse lines)
-      (loop (cons l lines)))))
+(define fp (open-input-file "input"))
 
 (define lines (get-lines fp))
 

+ 2 - 8
advent-of-code/2023/01/2.rkt

@@ -1,14 +1,8 @@
 #lang racket
 
-(define fp (open-input-file "input"))
-(define sum 0)
+(require "../lib/utils.rkt")
 
-(define (get-lines fp)
-  (let loop ((lines '()))
-    (define l (read-line fp))
-    (if (eof-object? l)
-      (reverse lines)
-      (loop (cons l lines)))))
+(define fp (open-input-file "input"))
 
 (define lines (get-lines fp))
 

+ 2 - 13
advent-of-code/2023/02/1.rkt

@@ -1,13 +1,8 @@
 #lang racket
 
-(define fp (open-input-file "input"))
+(require "../lib/utils.rkt")
 
-(define (get-lines fp)
-  (let loop ((lines '()))
-    (define l (read-line fp))
-    (if (eof-object? l)
-      (reverse lines)
-      (loop (cons l lines)))))
+(define fp (open-input-file "input"))
 
 (define lines (get-lines fp))
 
@@ -51,12 +46,6 @@
         #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)))

+ 2 - 7
advent-of-code/2023/02/2.rkt

@@ -1,13 +1,8 @@
 #lang racket
 
-(define fp (open-input-file "input"))
+(require "../lib/utils.rkt")
 
-(define (get-lines fp)
-  (let loop ((lines '()))
-    (define l (read-line fp))
-    (if (eof-object? l)
-      (reverse lines)
-      (loop (cons l lines)))))
+(define fp (open-input-file "input"))
 
 (define lines (get-lines fp))
 

+ 1 - 7
advent-of-code/2023/04/1.rkt

@@ -1,14 +1,8 @@
 #lang racket
 
+(require "../lib/utils.rkt")
 (require "../lib/obj.rkt")
 
-(define (get-lines fp)
-  (let loop ((lines '()))
-    (define l (read-line fp))
-    (if (eof-object? l)
-      (reverse lines)
-      (loop (cons l lines)))))
-
 (define fp (open-input-file "input"))
 
 (define lines (get-lines fp))

+ 1 - 7
advent-of-code/2023/04/2.rkt

@@ -1,14 +1,8 @@
 #lang racket
 
+(require "../lib/utils.rkt")
 (require "../lib/obj.rkt")
 
-(define (get-lines fp)
-  (let loop ((lines '()))
-    (define l (read-line fp))
-    (if (eof-object? l)
-      (reverse lines)
-      (loop (cons l lines)))))
-
 (define fp (open-input-file "input"))
 
 (define lines (get-lines fp))

+ 1 - 1
advent-of-code/2023/04/input

@@ -194,4 +194,4 @@ Card 193: 13 33 98 37 19 86 32 15 95 96 | 56 90  5 60 24 21 46 73 29  3 58 75 77
 Card 194: 77  6 10 48 14 79 73 51 49 25 | 86 12 37 23 43 34  5 89 97 27 53 70 75 19 15 79 45 26  1 73 68 36  2 78 18
 Card 195: 94 57 24 37 46 75 73 10 29  5 | 78 25 21 48 22 46 38 76 19 17 64 32 88 99 63 12 20 41 16  7 14 54 81 97 89
 Card 196: 76 48 15 89 44 50 79 80 52 78 | 93 55 21 18 73 31 47 20 97 83 87 30  6 24 77 74 67 45 76 65 37 43 42 98 38
-Card 197: 67 21 75 10  9  6 47 88 45 70 | 91 95 58 82 52 50 87 81 78 13 64 53 57 14 55 25 36 76 19 86 56  2 16 54  1
+Card 197: 67 21 75 10  9  6 47 88 45 70 | 91 95 58 82 52 50 87 81 78 13 64 53 57 14 55 25 36 76 19 86 56  2 16 54  1

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

@@ -9,7 +9,7 @@
           (list-ref (string-split (read-line port) ":") 1)))
     (map string->number (string-split nums-str " "))))
 
-(define _ (read-line port))
+(begin (read-line port) (void))
 
 (define (read-line-convert-eof port)
   (define line (read-line port))

+ 1 - 1
advent-of-code/2023/05/part2.rkt → advent-of-code/2023/05/2.rkt

@@ -21,7 +21,7 @@
     (reverse 
       (pairing (map string->number (string-split nums-str " "))))))
 
-(define _ (read-line port))
+(begin (read-line port) (void))
 
 (define (read-line-convert-eof port)
   (define line (read-line port))

+ 32 - 0
advent-of-code/2023/lib/obj.rkt

@@ -0,0 +1,32 @@
+#lang racket
+
+(provide obj-maker
+         obj-set
+         obj-show)
+
+(define (alist->obj alist)
+  (lambda key
+    (if (null? key)
+      alist
+      (cadr (assoc (car key) alist)))))
+
+(define (obj-maker . fields)
+  (lambda inits
+    (define alist (map list fields inits))
+    (alist->obj alist)))
+
+(define (obj-set record key value)
+  (define alist (record))
+  (define new-alist
+    (let loop ((new-list '()) (cur alist) (is-set #f))
+      (if (null? cur)
+        (if is-set
+          new-list
+          (cons (list key value) new-list))
+        (let ()
+          (if (eq? key (caar cur))
+            (loop (cons (list key value) new-list) (cdr cur) #t)
+            (loop (cons (car cur) new-list) (cdr cur) is-set))))))
+  (alist->obj new-alist))
+
+(define (obj-show x) (x))

+ 17 - 0
advent-of-code/2023/lib/utils.rkt

@@ -0,0 +1,17 @@
+#lang racket
+
+(provide get-lines
+         enumerate)
+
+(define (get-lines fp)
+  (let loop ((lines '()))
+    (define l (read-line fp))
+    (if (eof-object? l)
+      (reverse lines)
+      (loop (cons l lines)))))
+
+(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)))))