diff options
| author | Mistivia <i@mistivia.com> | 2024-02-17 12:27:24 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-02-17 12:27:24 +0800 |
| commit | 8fcd7e1ed30a8b04d4d658b2d8c6acad938424d4 (patch) | |
| tree | 1cf064396cb0ddfdf77b2d3d6b56ebdd27c0ba9b /advent-of-code/lib | |
| parent | 050fa7cbfb6b7cf293fb02e06daf123b3e6af816 (diff) | |
add aoc 2022
Diffstat (limited to 'advent-of-code/lib')
| -rw-r--r-- | advent-of-code/lib/obj.rkt | 32 | ||||
| -rw-r--r-- | advent-of-code/lib/utils.rkt | 36 |
2 files changed, 68 insertions, 0 deletions
diff --git a/advent-of-code/lib/obj.rkt b/advent-of-code/lib/obj.rkt new file mode 100644 index 0000000..578ea36 --- /dev/null +++ b/advent-of-code/lib/obj.rkt @@ -0,0 +1,32 @@ +#lang racket/base + +(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/lib/utils.rkt b/advent-of-code/lib/utils.rkt new file mode 100644 index 0000000..1e777c8 --- /dev/null +++ b/advent-of-code/lib/utils.rkt @@ -0,0 +1,36 @@ +#lang racket/base + +(provide get-lines + enumerate + repeat + split-list-by) + +(define (repeat n e) + (let loop ((i 0) (ret '())) + (if (>= i n) + ret + (loop (+ 1 i) (cons e ret))))) + +(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))))) + +(define (split-list-by e lst . eq) + (define cmp (if (null? eq) equal? eq)) + (let loop ((ret '()) + (cur '()) + (lst lst)) + (if (null? lst) + (reverse (cons (reverse cur) ret)) + (if (cmp e (car lst)) + (loop (cons (reverse cur) ret) '() (cdr lst)) + (loop ret (cons (car lst) cur) (cdr lst))))))
\ No newline at end of file |
