From 05a24a6ac49f5b51da3cdbc0a44409466563ff95 Mon Sep 17 00:00:00 2001 From: xenia Date: Wed, 6 Dec 2023 01:48:14 -0500 Subject: [PATCH] day 6: meow --- 6.rkt | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/6.rkt b/6.rkt index ae64182..a7afc17 100644 --- a/6.rkt +++ b/6.rkt @@ -1,39 +1,32 @@ #lang curly-fn racket -(require "scripts/aoc.rkt") +(require "scripts/aoc.rkt" math/number-theory) ;; solution for day 6 -(define (do-race total-time hold-time) - (define rem-time (- total-time hold-time)) - (* rem-time hold-time)) +(define *fudge* 1e-10) + +(define (do-race time dist) + (match-define (list lower upper) (quadratic-solutions 1 (- time) dist)) + (~> (- (floor (- upper *fudge*)) (ceiling (+ lower *fudge*))) + add1 inexact->exact)) (define (part1 input) - (apply - * - (for/list ([time (in-list (first input))] [dist (in-list (second input))]) - (for/fold ([cnt 0]) ([hold (in-range time)]) - (define res (do-race time hold)) - (if (> res dist) - (add1 cnt) - cnt))))) + (for/product ([time (in-list (first input))] + [dist (in-list (second input))]) + (do-race time dist))) -(define (cvt l) - (string->number (apply string-append (map number->string l)))) +(define (number-concat l) + (~>> (map number->string l) + (apply string-append) + string->number)) (define (part2 input) - (define time (cvt (first input))) - (define dist (cvt (second input))) - (for/fold ([cnt 0]) ([hold (in-range time)]) - (define res (do-race time hold)) - (if (> res dist) - (add1 cnt) - cnt))) + (apply do-race (map number-concat input))) (define (parse fname) - (match-define (list time-str dist-str) (file->lines fname)) - (list (map string->number (rest (string-split time-str))) - (map string->number (rest (string-split dist-str))))) + (map #{map string->number (rest (string-split %))} + (file->lines fname))) ; (dbg (part1 (parse "inputs/6-test1"))) ; (error)