You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

106 lines
5.0 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

#lang scribble/lp2
@(require scribble/manual aoc-racket/helper)
@link[""]{The puzzle}. Our @link-rp["day14-input.txt"]{input} is a list of flying-reindeer descriptions  in particular, how fast they can fly, and how long they have to rest between flight sessions, e.g., @italic{Dancer can fly 27 km/s for 5 seconds, but then must rest for 132 seconds.}
@isection{After 2503 seconds, what is the maximum distance any reindeer has flown?}
For each reindeer, we have a description that specifies a) flight speed, b) flight time, and c) rest time. Thus, if we have the total flight time as input  and we do we can use it to calculate flight distance. In other words, just as we made functions out of wire descriptions on @secref{Day_7}, now we'll make functions out of reindeer descriptions.
As in @secref{Day_7}, we'll use @iracket[define-syntax] to set up the reindeer functions. Each reindeer function will take a time in seconds and then use @racket[calc-distance] to convert it into meters. After that, all we need to do is @iracket[map] the time over the reindeer and @iracket[max] to get the answer.
(require racket rackunit (for-syntax racket/file))
(provide (all-defined-out))
(define-syntax (convert-input-to-reindeer-functions stx)
(syntax-case stx ()
(let* ([input-strings (file->lines "day14-input.txt")]
(map (λ(str) (format "(reindeer ~a)" (string-downcase str))) input-strings)]
(map (compose1 read open-input-string) reindeer-strings)])
(datum->syntax stx `(begin ,@reindeer-datums)))]))
(define-syntax (reindeer stx)
(syntax-case stx (can fly seconds but then must rest for)
[(_ deer-name can fly speed km/s for fly-secs seconds, but then must rest for rest-secs seconds.)
#'(define (deer-name total-secs)
(calc-distance total-secs speed fly-secs rest-secs))]
[else #'(void)]))
(define (calc-distance total-secs speed fly-secs rest-secs)
(let loop ([secs-remaining total-secs][distance 0])
(if (<= secs-remaining 0)
(let ([secs-in-flight (min secs-remaining fly-secs)])
(loop (- secs-remaining fly-secs rest-secs)
(+ (* secs-in-flight speed) distance))))))
(define (q1)
(define seconds-to-travel 2503)
(apply max (map (λ(deer-func) (deer-func seconds-to-travel))
(list dasher dancer prancer vixen comet
cupid donner blitzen rudolph))))]
@isection{Under the new rule, how many points does the winning reindeer have?}
The new rule is that after each second of travel, the reindeer in the lead gets one point. Thus, the winner after 2503 seconds is not the reindeer that has traveled farthest, but that has gathered the most points  in other words, has been in the lead for the longest time.
This question is similar to the last. But instead of simulating one race, we have to simulate 2503 races, each one ending a second later than the last. After each second, we calculate the winning reindeer, and add it to our list of winners. After 2503 seconds, we find out how many times the winningest reindeer appears on the list. To do this, we'll use the helper function @iracket[frequency-hash] from @racketmodname[sugar/list]. (You could also do it with @iracket[for/fold], but managing nine reindeer in parallel is unwieldy. Just ask Santa.)
(require sugar/list)
(define (q2)
(define deer-funcs (list dasher dancer prancer vixen comet
cupid donner blitzen rudolph))
(define winners
(for/list ([sec (in-range 1 (add1 2503))])
(define deer-results
(map (λ(deer-func) (deer-func sec)) deer-funcs))
(define max-result (apply max deer-results))
(map (λ(deer-result deer-func)
(if (= deer-result max-result)
deer-results deer-funcs)))))
(apply max (hash-values winners)))
@section{Testing Day 14}
(module+ test
(define input-str (file->string "day14-input.txt"))
(check-equal? (q1) 2640)
(check-equal? (q2) 1102))]