aboutsummaryrefslogtreecommitdiff
path: root/2021/day-07/day-07.rkt
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2021-12-09 18:05:04 -0500
committerHJ <thechairman@thechairman.info>2021-12-09 18:05:04 -0500
commit527bc4762f9c66c9244f0ac1fbee6357478ac9ef (patch)
treeecd3ffc97de5aab201699f93aab1e0417ac3d25d /2021/day-07/day-07.rkt
downloadgleam_aoc-527bc4762f9c66c9244f0ac1fbee6357478ac9ef.tar.gz
gleam_aoc-527bc4762f9c66c9244f0ac1fbee6357478ac9ef.zip
putting all my solutions together in 1 repository
Diffstat (limited to '2021/day-07/day-07.rkt')
-rw-r--r--2021/day-07/day-07.rkt30
1 files changed, 30 insertions, 0 deletions
diff --git a/2021/day-07/day-07.rkt b/2021/day-07/day-07.rkt
new file mode 100644
index 0000000..81a2e4f
--- /dev/null
+++ b/2021/day-07/day-07.rkt
@@ -0,0 +1,30 @@
+#lang racket
+(require advent-of-code
+ threading
+ math/statistics)
+
+(define crab-data
+ (~> (open-aoc-input (find-session) 2021 7 #:cache #t)
+ port->string
+ string-trim
+ (string-split ",")
+ (map string->number _)))
+
+(define (gauss-sum n) (/ (* n (+ n 1)) 2))
+(define (compute-fuel-use f crabs align-to)
+ (for/sum ([crab (in-list crabs)])
+ (f (abs (- crab align-to)))))
+
+;; using the fact that the optimum location is at the median
+;; of the crabs' starting location for the linear relationship
+;; and at a coordinate within 1 unit of the mean for the quadratic one
+
+(~>> crab-data
+ (median <)
+ (compute-fuel-use identity crab-data))
+
+(~>> crab-data
+ mean
+ ((λ (m) (list (floor m) (ceiling m))))
+ (map (curry compute-fuel-use gauss-sum crab-data))
+ (apply min)) \ No newline at end of file