aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/lib
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-02-15 18:42:35 +0800
committerMistivia <i@mistivia.com>2024-02-15 18:42:35 +0800
commita19a1b970e5dd6983be8660ef6e0f5929fb5a149 (patch)
treeae3e6b3b68573df77b872a2470a7dc01a8d69749 /advent-of-code/2023/lib
parentf163146fe4b31f1b9e4e25f7f61a5c6928e2fe5b (diff)
refactor
Diffstat (limited to 'advent-of-code/2023/lib')
-rw-r--r--advent-of-code/2023/lib/obj.rkt32
-rw-r--r--advent-of-code/2023/lib/utils.rkt17
2 files changed, 49 insertions, 0 deletions
diff --git a/advent-of-code/2023/lib/obj.rkt b/advent-of-code/2023/lib/obj.rkt
new file mode 100644
index 0000000..9217976
--- /dev/null
+++ b/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)) \ No newline at end of file
diff --git a/advent-of-code/2023/lib/utils.rkt b/advent-of-code/2023/lib/utils.rkt
new file mode 100644
index 0000000..946bb86
--- /dev/null
+++ b/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))))) \ No newline at end of file