-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.rkt
27 lines (19 loc) · 812 Bytes
/
day1.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#lang racket
(require data/maybe point-free)
(provide part1 part2)
(define/compose get-input (curry map string->number) file->lines)
(define input (get-input "input/d1"))
(define (part1) (apply * (from-just! (find-pair input 2020))))
(define (part2) (apply * (from-just! (find-triplet input 2020))))
(define (find-pair entries sum)
(match entries
['() nothing]
[(list x xs ...) (let ([y (- sum x)]) (if (member y xs)
(just (list x y))
(find-pair xs sum)))]))
(define (find-triplet entries sum)
(match entries
['() nothing]
[(list x xs ...) (match (find-pair xs (- sum x))
[(just (list y z)) (just (list x y z))]
[nothing (find-triplet xs sum)])]))