aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-18 08:06:09 -0500
committerHJ <thechairman@thechairman.info>2023-12-18 08:06:09 -0500
commitd782a6118d7c48371912d52c1839014d0309ff5a (patch)
treec5b753d124965ea3a020ca6a785ca3c46c62d30d
parent33ed65c88973597c07409a151fd5ced3cf6c5cef (diff)
downloadgleam_aoc-d782a6118d7c48371912d52c1839014d0309ff5a.tar.gz
gleam_aoc-d782a6118d7c48371912d52c1839014d0309ff5a.zip
day 18 racket complete
-rw-r--r--aoc2023-other/day-18/day-18.rkt51
1 files changed, 51 insertions, 0 deletions
diff --git a/aoc2023-other/day-18/day-18.rkt b/aoc2023-other/day-18/day-18.rkt
new file mode 100644
index 0000000..4d3e95b
--- /dev/null
+++ b/aoc2023-other/day-18/day-18.rkt
@@ -0,0 +1,51 @@
+#lang racket
+(require advent-of-code
+ threading)
+
+(struct coord (x y) #:transparent)
+
+(define input (~> (fetch-aoc-input (find-session) 2023 18 #:cache #true)))
+
+(define (go-to-next-coord c dir dist)
+ (match-define (coord x y) c)
+ (match dir
+ ["R" (coord (+ x dist) y)]
+ ["L" (coord (- x dist) y)]
+ ["U" (coord x (+ y dist))]
+ ["D" (coord x (- y dist))]))
+
+(define/match (triangle-area _coord1 _coord2)
+ [((coord x1 y1) (coord x2 y2)) (/ (- (* x1 y2) (* x2 y1)) 2)])
+
+(define (find-area-using parser)
+ (for/fold ([area 0]
+ [perimeter 0]
+ [current-coord (coord 0 0)]
+ #:result (+ 1 (abs area) (/ perimeter 2)))
+ ([dig (in-list (string-split input "\n"))])
+ (define-values (dir dist) (parser dig))
+ (define next-coord (go-to-next-coord current-coord dir dist))
+ (values (+ area (triangle-area current-coord next-coord)) (+ perimeter dist) next-coord)))
+
+;; part 1
+(define (parse-front dig)
+ (match-define (regexp #rx"(.) (.*) \\((.*)\\)" (list _ dir (app string->number dist) _hex)) dig)
+ (values dir dist))
+
+(find-area-using parse-front)
+
+;; part 2
+
+(define (parse-hex dig)
+ (match-define (regexp #rx".*\\(#(.....)(.)\\)"
+ (list _ (app (curryr string->number 16) dist) (app num->dir dir)))
+ dig)
+ (values dir dist))
+
+(define/match (num->dir _n)
+ [("0") "R"]
+ [("1") "D"]
+ [("2") "L"]
+ [("3") "U"])
+
+(find-area-using parse-hex)