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
(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)