diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2024-02-02 17:05:12 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2024-02-02 17:05:12 -0500 |
commit | 48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9 (patch) | |
tree | f59a13e0b5e80ab925220b4488c6e36b1bec660a /aoc2023-racket/day-06/day-06.rkt | |
parent | 87e9ab25ff70e215b537939a4bc23ab101f41dbe (diff) | |
download | gleam_aoc-48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9.tar.gz gleam_aoc-48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9.zip |
renaming
Diffstat (limited to 'aoc2023-racket/day-06/day-06.rkt')
-rw-r--r-- | aoc2023-racket/day-06/day-06.rkt | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/aoc2023-racket/day-06/day-06.rkt b/aoc2023-racket/day-06/day-06.rkt new file mode 100644 index 0000000..53ca9ee --- /dev/null +++ b/aoc2023-racket/day-06/day-06.rkt @@ -0,0 +1,32 @@ +#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))) |