-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.rkt
47 lines (37 loc) · 1.28 KB
/
day10.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
43
44
45
46
47
#lang racket
(require "aoc.rkt"
match-plus)
(define (positions data t)
(for/list ([line (in-list data)])
(match-define (list x y dx dy) line)
(list (+ x (* dx t)) (+ y (* dy t)))))
(define (boundaries positions)
(list
(apply min (map first positions))
(apply max (map first positions))
(apply min (map second positions))
(apply max (map second positions))))
(define/match* (area (list x-min x-max y-min y-max))
(* (- x-max x-min) (- y-max y-min)))
(define (solve data)
(define (size data t)
(area (boundaries (positions data t))))
(define initial-wait 10000) ; positions are ~10_000x larger than velocities
(for/fold ([t 0]
[prev-size +inf.0]
#:result (sub1 t))
([t (in-naturals initial-wait)])
(define sz (size data t))
#:break (> sz prev-size)
(values t sz)))
(define (show-message data time)
(define stars (positions data time))
(match-define (list x-min x-max y-min y-max) (boundaries stars))
(for ([y (in-inclusive-range y-min y-max)])
(for ([x (in-inclusive-range x-min x-max)])
(display (if (member (list x y) stars) #\# #\space)))
(newline)))
(define data (map extract-numbers (read-input 10)))
(define waiting-time (solve data))
(show-message data waiting-time)
waiting-time