-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-18.rkt
35 lines (28 loc) · 1.22 KB
/
day-18.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
#lang racket
(require advent-of-code
threading
graph)
(define input
(~> (fetch-aoc-input (find-session) 2024 18 #:cache #true)
(string-split "\n")
(map (λ~> (string-split _ ",") (map string->number _) (apply cons _)) _)))
(define MAX-SIZE 70)
(define (make-empty-graph)
(~> (for*/list ([x (in-inclusive-range 0 MAX-SIZE)]
[y (in-inclusive-range 0 MAX-SIZE)]
[delta (in-list (list (cons 0 1) (cons 1 0) (cons -1 0) (cons 0 -1)))]
#:do [(define neighbor (cons (+ x (car delta)) (+ y (cdr delta))))]
#:when (and (<= 0 (car neighbor) MAX-SIZE) (<= 0 (cdr neighbor) MAX-SIZE)))
(list (cons x y) neighbor))
unweighted-graph/undirected))
;; part 1
(define starting-graph (make-empty-graph))
(for ([obstacle (in-list (take input 1024))])
(remove-vertex! starting-graph obstacle))
(~> starting-graph (fewest-vertices-path (cons 0 0) (cons MAX-SIZE MAX-SIZE)) length sub1)
;; part 2
(define obstacles (drop input 1024))
(for/first ([obstacle (in-list obstacles)]
#:do [(remove-vertex! starting-graph obstacle)]
#:unless (fewest-vertices-path starting-graph (cons 0 0) (cons MAX-SIZE MAX-SIZE)))
obstacle)