62 lines
1.4 KiB
Racket
62 lines
1.4 KiB
Racket
|
#lang curly-fn racket
|
||
|
|
||
|
(require "scripts/aoc.rkt" parser-tools/lex parser-tools/yacc (prefix-in : parser-tools/lex-sre))
|
||
|
|
||
|
;; solution for day 18
|
||
|
|
||
|
(define-tokens nums [number])
|
||
|
(define-empty-tokens syms [+ * lp rp eof])
|
||
|
|
||
|
(define the-lexer
|
||
|
(lexer
|
||
|
[whitespace (the-lexer input-port)]
|
||
|
["+" (token-+)]
|
||
|
["*" (token-*)]
|
||
|
["(" (token-lp)]
|
||
|
[")" (token-rp)]
|
||
|
[(:+ (char-set "0123456789")) (token-number (string->number lexeme))]
|
||
|
[(eof) (token-eof)]))
|
||
|
|
||
|
(define the-parser
|
||
|
(parser
|
||
|
[start exp]
|
||
|
[end eof]
|
||
|
[error (lambda args (error "meow" args))]
|
||
|
[tokens nums syms]
|
||
|
[precs (left *)(left +)
|
||
|
(left lp rp)]
|
||
|
[grammar
|
||
|
(exp [(lp exp rp) $2]
|
||
|
[(exp + exp) (+ $1 $3)]
|
||
|
[(exp * exp) (* $1 $3)]
|
||
|
[(number) $1])]))
|
||
|
|
||
|
(define (parse/string str)
|
||
|
(let ([input (open-input-string str)])
|
||
|
(the-parser (lambda () (the-lexer input)))))
|
||
|
|
||
|
;; broken!
|
||
|
(define (part1 input)
|
||
|
(for/sum ([line (in-list input)])
|
||
|
(parse/string line)))
|
||
|
|
||
|
(define (part2 input)
|
||
|
(for/sum ([line (in-list input)])
|
||
|
(parse/string line)))
|
||
|
|
||
|
;; parse input file
|
||
|
(define (parse fname)
|
||
|
(define input (file->lines fname))
|
||
|
input)
|
||
|
|
||
|
(module+ test
|
||
|
(require rackunit)
|
||
|
;; tests here
|
||
|
(displayln "no tests :("))
|
||
|
|
||
|
(module+ main
|
||
|
(define input (parse "inputs/18"))
|
||
|
(answer 18 1 (time (part1 input)))
|
||
|
(answer 18 2 (time (part2 input)))
|
||
|
(displayln "meow"))
|