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