aboutsummaryrefslogtreecommitdiff
path: root/2021/day2/02b.rkt
blob: 7688c1d43e3054925c6f7172d43eea5a05865dcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)