day 6: meow

This commit is contained in:
xenia 2023-12-06 01:48:14 -05:00
parent 08fbef4592
commit 05a24a6ac4
1 changed files with 17 additions and 24 deletions

41
6.rkt
View File

@ -1,39 +1,32 @@
#lang curly-fn racket #lang curly-fn racket
(require "scripts/aoc.rkt") (require "scripts/aoc.rkt" math/number-theory)
;; solution for day 6 ;; solution for day 6
(define (do-race total-time hold-time) (define *fudge* 1e-10)
(define rem-time (- total-time hold-time))
(* rem-time hold-time)) (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) (define (part1 input)
(apply (for/product ([time (in-list (first input))]
* [dist (in-list (second input))])
(for/list ([time (in-list (first input))] [dist (in-list (second input))]) (do-race time dist)))
(for/fold ([cnt 0]) ([hold (in-range time)])
(define res (do-race time hold))
(if (> res dist)
(add1 cnt)
cnt)))))
(define (cvt l) (define (number-concat l)
(string->number (apply string-append (map number->string l)))) (~>> (map number->string l)
(apply string-append)
string->number))
(define (part2 input) (define (part2 input)
(define time (cvt (first input))) (apply do-race (map number-concat 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)))
(define (parse fname) (define (parse fname)
(match-define (list time-str dist-str) (file->lines fname)) (map #{map string->number (rest (string-split %))}
(list (map string->number (rest (string-split time-str))) (file->lines fname)))
(map string->number (rest (string-split dist-str)))))
; (dbg (part1 (parse "inputs/6-test1"))) ; (dbg (part1 (parse "inputs/6-test1")))
; (error) ; (error)