aoc2020/18.rkt

53 lines
1.3 KiB
Racket

#lang curly-fn racket
(require "scripts/aoc.rkt" parser-tools/lex parser-tools/yacc)
;; solution for day 18
(define-tokens nums [number])
(define-empty-tokens syms [+ * lp rp eof])
(define oo-lexer
(lexer
[whitespace (oo-lexer input-port)]
["+" (token-+)]
["*" (token-*)]
["(" (token-lp)]
[")" (token-rp)]
[numeric (token-number (string->number lexeme))]
[(eof) (token-eof)]))
(define-simple-macro (make-oo-parser/precs needed-precs ...)
(parser
[start exp]
[end eof]
[error (lambda args (error "not shonks:" args))]
[tokens nums syms]
[precs needed-precs ...
(left lp rp)]
[grammar
(exp [(lp exp rp) $2]
[(exp + exp) (+ $1 $3)]
[(exp * exp) (* $1 $3)]
[(number) $1])]))
(define (parse/string oo-parser str)
(let ([input (open-input-string str)])
(oo-parser (thunk (oo-lexer input)))))
(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-part part1 (left + *))
(define-part part2 (left *) (left +))
(define parse file->lines)
(module+ main
(define input (parse "inputs/18"))
(answer 18 1 (time (part1 input)))
(answer 18 2 (time (part2 input)))
(displayln "meow"))