forked from frerich/aoc2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18.rck
More file actions
24 lines (20 loc) · 734 Bytes
/
day18.rck
File metadata and controls
24 lines (20 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#lang curly-fn racket
(define input
(map #{read (open-input-string (format "(~a)" %))} (problem-input 18)))
(define (eval1 sexp)
(match sexp
[`,n #:when (number? n) n]
[`(,tail) (eval1 tail)]
[`(,rest ... + ,tail)
(+ (eval1 tail) (eval1 rest))]
[`(,rest ... * ,tail)
(* (eval1 tail) (eval1 rest))]))
(define (eval2 sexp)
(match sexp
[`,n #:when (number? n) n]
[`(,tail) (eval2 tail)]
[`(,head ... ,left + ,right ,tail ...)
(eval2 `(,@head ,(+ (eval2 left) (eval2 right)) ,@tail))]
[`(,head ... ,left * ,right ,tail ...)
(eval2 `(,@head ,(* (eval2 left) (eval2 right)) ,@tail))]))
(printf "Part 1: ~a\nPart 2: ~a\n" (apply + (map eval1 input)) (apply + (map eval2 input)))