You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
aoc-racket/2020/18.rkt

39 lines
1.0 KiB
Racket

#lang at-exp br
(require racket/file rackunit br/module)
@module/lang[parser1]{
#lang brag
op : [op ("+" | "*")] term
@"@"term : /"(" op /")" | digit
@"@"digit : "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
}
(require (prefix-in parser1: 'parser1))
(define (op . xs)
(match xs
[(list left opstr right) ((if (equal? opstr "*") * +) left (op right))]
[(list (? number? num)) num]
[(list (app string->number num)) num]))
(define-namespace-anchor ns)
(define ((solve parser) str)
(eval (parser (regexp-match* #px"\\S" str)) (namespace-anchor->namespace ns)))
(check-equal? (apply + (map (solve parser1:parse) (file->lines "18.rktd"))) 9535936849815)
@module/lang[parser2]{
#lang brag
prod : [prod "*"] sum
sum : [sum "+"] term
@"@"term : /"(" prod /")" | digit
@"@"digit : "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
}
(define prod op)
(define sum op)
(require (prefix-in parser2: 'parser2))
(check-equal?
(apply + (map (solve parser2:parse) (file->lines "18.rktd"))) 472171581333710)