aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-other/day-17/day-17.rkt
blob: 8a1fc0286e972f033925ce38520f5b96fea71192 (plain)
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
43
44
45
46
47
48
49
50
51
#lang racket

(require advent-of-code
         threading)

(struct posn (r c) #:transparent)
(struct to (posn dir) #:transparent)
(struct edge (from to dir) #:transparent)

(define input
  "2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533")

(define grid
  (for*/hash ([(row r) (in-indexed (in-list (string-split input "\n")))]
              [(col c) (in-indexed (in-string row))])
    (values (posn r c) (~> col string string->number))))

(define grid-nodes (hash-keys grid))
(match-define (posn rmax cmax) (argmax (λ (p) (+ (posn-r p) (posn-c p))) grid-nodes))

(define/match (find-edge-cost e prev-two)
  [((edge _ _ d) (list d d)) 1e10]
  [(_ _) (hash-ref edges e)])

(define (find-node-cost p)
  (match-define (posn r c) p)
  (+ (- rmax r) (- cmax c)))

(define (neighbors p)
  (match-define (posn r c) p)
  (~>> (list (to (posn (sub1 r) c) 'north)
             (to (posn (add1 r) c) 'south)
             (to (posn r (add1 c)) 'east)
             (to (posn r (sub1 c)) 'west))
       (filter (λ (p) (hash-has-key? grid (to-posn p))))))

(define edges
  (for*/hash ([(k v) (in-hash grid)] [neighbor (in-list (neighbors k))])
    (values (edge k (to-posn neighbor) (to-dir neighbor)) v)))