day 18: now both parts work
This commit is contained in:
parent
b2aecd8567
commit
f0f426ddfc
37
18.rkt
37
18.rkt
|
@ -7,9 +7,9 @@
|
|||
(define-tokens nums [number])
|
||||
(define-empty-tokens syms [+ * lp rp eof])
|
||||
|
||||
(define the-lexer
|
||||
(define oo-lexer
|
||||
(lexer
|
||||
[whitespace (the-lexer input-port)]
|
||||
[whitespace (oo-lexer input-port)]
|
||||
["+" (token-+)]
|
||||
["*" (token-*)]
|
||||
["(" (token-lp)]
|
||||
|
@ -17,13 +17,13 @@
|
|||
[(:+ (char-set "0123456789")) (token-number (string->number lexeme))]
|
||||
[(eof) (token-eof)]))
|
||||
|
||||
(define the-parser
|
||||
(define-simple-macro (make-oo-parser/precs needed-precs ...)
|
||||
(parser
|
||||
[start exp]
|
||||
[end eof]
|
||||
[error (lambda args (error "meow" args))]
|
||||
[error (lambda args (error "not shonks:" args))]
|
||||
[tokens nums syms]
|
||||
[precs (left *)(left +)
|
||||
[precs needed-precs ...
|
||||
(left lp rp)]
|
||||
[grammar
|
||||
(exp [(lp exp rp) $2]
|
||||
|
@ -31,28 +31,19 @@
|
|||
[(exp * exp) (* $1 $3)]
|
||||
[(number) $1])]))
|
||||
|
||||
(define (parse/string str)
|
||||
(define (parse/string oo-parser str)
|
||||
(let ([input (open-input-string str)])
|
||||
(the-parser (lambda () (the-lexer input)))))
|
||||
(oo-parser (thunk (oo-lexer input)))))
|
||||
|
||||
;; broken!
|
||||
(define (part1 input)
|
||||
(for/sum ([line (in-list input)])
|
||||
(parse/string line)))
|
||||
(define-simple-macro (define-part name:id needed-precs ...)
|
||||
(define (name input)
|
||||
(for/sum ([line (in-list input)])
|
||||
(parse/string (make-oo-parser/precs needed-precs ...) line))))
|
||||
|
||||
(define (part2 input)
|
||||
(for/sum ([line (in-list input)])
|
||||
(parse/string line)))
|
||||
(define-part part1 (left + *))
|
||||
(define-part part2 (left *) (left +))
|
||||
|
||||
;; parse input file
|
||||
(define (parse fname)
|
||||
(define input (file->lines fname))
|
||||
input)
|
||||
|
||||
(module+ test
|
||||
(require rackunit)
|
||||
;; tests here
|
||||
(displayln "no tests :("))
|
||||
(define parse file->lines)
|
||||
|
||||
(module+ main
|
||||
(define input (parse "inputs/18"))
|
||||
|
|
Loading…
Reference in New Issue