aoc2023/6.rkt

39 lines
937 B
Racket
Raw Normal View History

2023-12-06 06:29:20 +00:00
#lang curly-fn racket
2023-12-06 06:48:14 +00:00
(require "scripts/aoc.rkt" math/number-theory)
2023-12-06 06:29:20 +00:00
;; solution for day 6
2023-12-06 06:48:14 +00:00
(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))
2023-12-06 06:29:20 +00:00
(define (part1 input)
2023-12-06 06:48:14 +00:00
(for/product ([time (in-list (first input))]
[dist (in-list (second input))])
(do-race time dist)))
(define (number-concat l)
(~>> (map number->string l)
(apply string-append)
string->number))
2023-12-06 06:29:20 +00:00
(define (part2 input)
2023-12-06 06:48:14 +00:00
(apply do-race (map number-concat input)))
2023-12-06 06:29:20 +00:00
(define (parse fname)
2023-12-06 06:48:14 +00:00
(map #{map string->number (rest (string-split %))}
(file->lines fname)))
2023-12-06 06:29:20 +00:00
; (dbg (part1 (parse "inputs/6-test1")))
; (error)
(module+ main
(define input (parse "inputs/6"))
(answer 6 1 (time (part1 input)))
(answer 6 2 (time (part2 input)))
(displayln "meow"))