-
Notifications
You must be signed in to change notification settings - Fork 1
/
day09.rkt
42 lines (32 loc) · 914 Bytes
/
day09.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#lang racket
(require threading)
(define instructions
(~>> "inputs/09.txt"
file->lines
(map string->number)
list->vector))
(define (is-sum? prevs sum)
(for*/or ([a prevs]
[b prevs])
(= (+ a b) sum)))
(define (find-invalid [instr instructions])
(define (prevs idx)
(vector-take (vector-drop instr (- idx 25)) 25))
(for/first ([(val idx) (in-indexed instr)]
#:when (> idx 25)
#:unless (is-sum? (prevs idx) val))
val))
(define (sum-to goal [lo 0] [hi 2] [instr instructions])
(define slice
(~> instr
(vector-take hi)
(vector-drop lo)
vector->list))
(define total (apply + slice))
(cond
[(< total goal) (sum-to goal lo (add1 hi))]
[(> total goal) (sum-to goal (add1 lo) hi)]
[else (+ (apply min slice) (apply max slice))]))
(define invalid (find-invalid))
invalid
(sum-to invalid)