Advent of Code 2020 solutions in Racket, I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

#lang racket
(require "lib/common.rkt")
(define (parse prt)
(for/list ([str (in-list (port->lines prt))])
(read (open-input-string (format "(~a)" str)))))
(define (eval-arith expr)
(match expr
[n #:when (number? n) n]
[`(,tail) (eval-arith tail)]
[`(,rst ... + ,tail)
(+ (eval-arith rst) (eval-arith tail))]
[`(,rst ... * ,tail)
(* (eval-arith rst) (eval-arith tail))]))
(define (day18a input)
(for/sum ([expr (in-list input)])
(eval-arith expr)))
(define (eval-arith* expr)
(match expr
[n #:when (number? n) n]
[`(,tail) (eval-arith* tail)]
[`(,head ... ,left + ,right ,tail ...)
(eval-arith* `(,@head ,(+ (eval-arith* left) (eval-arith* right)) ,@tail))]
[`(,head ... ,left * ,right ,tail ...)
(eval-arith* `(,@head ,(* (eval-arith* left) (eval-arith* right)) ,@tail))]))
(define (day18b input)
(for/sum ([expr (in-list input)])
(eval-arith* expr)))
(module+ main
(call-with-input-file "data/day18.txt"
(λ (prt)
(define input (parse prt))
(dbg input)
(answer 18 1 (day18a input))
(answer 18 2 (day18b input)))))