aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-other
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-06 01:03:37 -0500
committerHJ <thechairman@thechairman.info>2023-12-06 01:03:37 -0500
commitba0980cb60c4373d28619a4b200b7375ac381791 (patch)
tree3645fa77c03b2d22bf4efc1007e0c44ffa37b52c /aoc2023-other
parentac7a889dea39a6479d2d6c47b2b8304bae60e49c (diff)
downloadgleam_aoc-ba0980cb60c4373d28619a4b200b7375ac381791.tar.gz
gleam_aoc-ba0980cb60c4373d28619a4b200b7375ac381791.zip
day 6 gleam/racket complete
Diffstat (limited to 'aoc2023-other')
-rw-r--r--aoc2023-other/day-06/day-06.rkt31
1 files changed, 31 insertions, 0 deletions
diff --git a/aoc2023-other/day-06/day-06.rkt b/aoc2023-other/day-06/day-06.rkt
new file mode 100644
index 0000000..2897821
--- /dev/null
+++ b/aoc2023-other/day-06/day-06.rkt
@@ -0,0 +1,31 @@
+#lang racket
+
+(require advent-of-code
+ threading)
+
+(match-define (list times distances)
+ (~> (open-aoc-input (find-session) 2023 6 #:cache #true) port->lines))
+
+;; part 1
+(define get-numbers (λ~> string-split (map string->number _) rest))
+(define (find-bound race-time dist button-time step)
+ (if (< dist (* button-time (- race-time button-time)))
+ button-time
+ (find-bound race-time dist (+ step button-time) step)))
+
+(define (lower-bound rtime dist)
+ (find-bound rtime dist 1 1))
+(define (upper-bound rtime dist)
+ (find-bound rtime dist rtime -1))
+
+(for/fold ([acc 1])
+ ([race-time (in-list (get-numbers times))] [distance (in-list (get-numbers distances))])
+ (* acc (add1 (- (upper-bound race-time distance) (lower-bound race-time distance)))))
+
+;; part 2
+
+(define unkern (λ~>> get-numbers (apply ~a) string->number))
+(define big-time (unkern times))
+(define big-distance (unkern distances))
+
+(add1 (- (upper-bound big-time big-distance) (lower-bound big-time big-distance)))