diff options
Diffstat (limited to '2021/day2')
-rw-r--r-- | 2021/day2/02a.rkt | 25 | ||||
-rw-r--r-- | 2021/day2/02b.rkt | 27 |
2 files changed, 52 insertions, 0 deletions
diff --git a/2021/day2/02a.rkt b/2021/day2/02a.rkt new file mode 100644 index 0000000..2317a9a --- /dev/null +++ b/2021/day2/02a.rkt @@ -0,0 +1,25 @@ +#lang racket + +(define (parse-line line) + (define split (string-split line " ")) + (cond [(empty? split) + (datum->syntax #f "")] + [else (define command (car split)) + (define arg (second split)) + (datum->syntax #f `(,(string->symbol command) ,(string->number arg)))])) + +(define (read-syntax path port) + (define src-datums (map parse-line (port->lines port))) + (datum->syntax #f `(module day2 racket + (define depth 0) + (define pos 0) + (define (forward x) + (set! pos (+ pos x))) + (define (down x) + (set! depth (+ depth x))) + (define (up x) + (set! depth (- depth x))) + ,@src-datums + (* depth pos)))) + +(provide read-syntax) diff --git a/2021/day2/02b.rkt b/2021/day2/02b.rkt new file mode 100644 index 0000000..7688c1d --- /dev/null +++ b/2021/day2/02b.rkt @@ -0,0 +1,27 @@ +#lang racket + +(define (parse-line line) + (define split (string-split line " ")) + (cond [(empty? split) + (datum->syntax #f "")] + [else (define command (car split)) + (define arg (second split)) + (datum->syntax #f `(,(string->symbol command) ,(string->number arg)))])) + +(define (read-syntax path port) + (define src-datums (map parse-line (port->lines port))) + (datum->syntax #f `(module day2 racket + (define depth 0) + (define pos 0) + (define aim 0) + (define (forward x) + (set! pos (+ pos x)) + (set! depth (+ depth (* aim x)))) + (define (down x) + (set! aim (+ aim x))) + (define (up x) + (set! aim (- aim x))) + ,@src-datums + (* depth pos)))) + +(provide read-syntax) |