blob: c31b64a64aa8cf394eb359616ce8cb264b18c2e5 (
plain) (
blame)
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
|
#lang racket
;; ala https://github.com/narimiran/AdventOfCode2022/blob/main/clojure/day15.clj
(define data
(for/list ([line (file->lines "day15")])
(map string->number (regexp-match* #rx"[0-9]+" line))))
(define (dist x1 y1 x2 y2)
(+ (abs (- x1 x2))
(abs (- y1 y2))))
(define (seen-in-row row)
(append-map (lambda (line)
(let* ([sx (first line)]
[sy (second line)]
[r (dist sx sy (third line) (fourth line))]
[d (- r (abs (- row sy)))])
(if (positive? d)
(list (list (- sx d) (+ sx d)))
(list))))
data))
(define (part1)
(let* ([seen (seen-in-row 2000000)]
[a (first (first (sort seen < #:key first)))]
[b (second (first (sort seen > #:key second)))])
(- b a)))
(part1)
;; 5147333
(define (find-a-hole seen)
(let loop ([highest 0]
[seen (sort seen < #:key first)])
(if (null? seen)
#f
(if (<= (caar seen) (+ highest 1))
(loop (max highest (cadar seen)) (rest seen))
(- (caar seen) 1)))))
(define (part2)
(let loop ([row 4000000])
(let* ([seen (seen-in-row row)]
[hole (find-a-hole seen)])
(if hole
(+ (* 4000000 hole) row)
(loop (- row 1))))))
(part2)
;; 13734006908372
|