-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-10.rkt
38 lines (30 loc) · 1.16 KB
/
day-10.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
#lang racket
(require advent-of-code
threading
racket/hash)
(struct Posn (x y) #:transparent)
(define (go a delta)
(Posn (+ (Posn-x a) (Posn-x delta)) (+ (Posn-y a) (Posn-y delta))))
(define in-cardinal-directions (list (Posn 1 0) (Posn -1 0) (Posn 0 1) (Posn 0 -1)))
(define (neighbors-of p with)
(map (curry go p) with))
(define input (fetch-aoc-input (find-session) 2024 10 #:cache #true))
(define GRID
(for*/hash ([(row y) (in-indexed (string-split input))]
[(col x) (in-indexed row)])
(values (Posn x y) (~> col string string->number))))
(define (look-for-summit current [summits '()])
(match (hash-ref GRID current)
[9 (cons current summits)]
[elev
(for/fold ([acc summits]) ([neighbor (neighbors-of current in-cardinal-directions)])
(if (equal? (add1 elev) (hash-ref GRID neighbor #f))
(look-for-summit neighbor acc)
acc))]))
(define (rate-trails using)
(define trailheads (~> GRID (hash-filter (λ (_ v) (= v 0))) hash-keys))
(for/sum ([trailhead trailheads]) (~> trailhead look-for-summit using)))
;; part 1
(rate-trails (λ~> list->set set-count))
;; part 2
(rate-trails length)