aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-other/day-24/day-24a.rkt
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-25 11:24:39 -0500
committerHJ <thechairman@thechairman.info>2023-12-25 11:24:39 -0500
commit40eb8465f7958ac056a3843d38234848b15464f7 (patch)
tree6a22b862107f219c6b1c354c4ac70792e9d41ce8 /aoc2023-other/day-24/day-24a.rkt
parentf3dfb53b1d59febe1f3bac746150372362f313a9 (diff)
downloadgleam_aoc-40eb8465f7958ac056a3843d38234848b15464f7.tar.gz
gleam_aoc-40eb8465f7958ac056a3843d38234848b15464f7.zip
day 1-24 end-of-year style cleanup
Diffstat (limited to 'aoc2023-other/day-24/day-24a.rkt')
-rw-r--r--aoc2023-other/day-24/day-24a.rkt20
1 files changed, 14 insertions, 6 deletions
diff --git a/aoc2023-other/day-24/day-24a.rkt b/aoc2023-other/day-24/day-24a.rkt
index 39f559c..31f526d 100644
--- a/aoc2023-other/day-24/day-24a.rkt
+++ b/aoc2023-other/day-24/day-24a.rkt
@@ -12,10 +12,13 @@
(define LOWER-BOUND 200000000000000)
(define UPPER-BOUND 400000000000000)
+(define (->struct f str)
+ (~> str (string-split _ ",") (map (λ~> string-trim string->number) _) (apply f _)))
+
(define (parse-hail-record str)
(match-define (list p v) (string-split str " @ "))
- (hail (~> p (string-split ",") (map (λ~> string-trim string->number) _) (apply posn _))
- (~> v (string-split ",") (map (λ~> string-trim string->number) _) (apply vel _))))
+ (hail (->struct posn p)
+ (->struct vel v)))
(define hail-paths
(for/list ([hail (in-list (string-split input "\n"))])
@@ -28,16 +31,21 @@
(cond
[(= (* vy1 vx2) (* vx1 vy2)) #f]
[else
- (define t1 (/ (- (* vy2 (- x1 x2)) (* vx2 (- y1 y2))) (- (* vy1 vx2) (* vx1 vy2))))
- (define t2 (/ (- (* vy1 (- x2 x1)) (* vx1 (- y2 y1))) (- (* vy2 vx1) (* vx2 vy1))))
+ (define t1 (/ (- (* vy2 (- x1 x2)) (* vx2 (- y1 y2)))
+ (- (* vy1 vx2) (* vx1 vy2))))
+ (define t2 (/ (- (* vy1 (- x2 x1)) (* vx1 (- y2 y1)))
+ (- (* vy2 vx1) (* vx2 vy1))))
(define x (+ x1 (* t1 vx1)))
(define y (+ y1 (* t1 vy1)))
- (and (<= LOWER-BOUND x UPPER-BOUND) (<= LOWER-BOUND y UPPER-BOUND) (<= 0 t1) (<= 0 t2))]))
+ (and (<= LOWER-BOUND x UPPER-BOUND)
+ (<= LOWER-BOUND y UPPER-BOUND)
+ (<= 0 t1)
+ (<= 0 t2))]))
(for/sum ([(trial-paths) (in-combinations hail-paths 2)] ;
#:when (apply valid-intersection? trial-paths))
- 1)
+ 1)
;; part 2 - see day-24b.rkt