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