Created
January 10, 2020 12:34
-
-
Save sotolf2/38ae8f07877fb35eb51b61bf4e92853a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require threading) | |
(define (get-input) | |
(file->lines "day14.txt")) | |
(struct raindeer (spd tm rst nm) #:transparent) | |
(define (parse-line str) | |
(define vals (map string->number (regexp-match* #px"\\d+" str))) | |
(define name (first (string-split str " "))) | |
(raindeer (first vals) (second vals) (third vals) name)) | |
(define (position-after rndr sec [return-name? #f]) | |
(define cycle-len (+ (raindeer-tm rndr) (raindeer-rst rndr))) | |
(define full-cycles (quotient sec cycle-len)) | |
(define dist-full-cycles (* (raindeer-spd rndr) (raindeer-tm rndr) full-cycles)) | |
(define extras (modulo sec cycle-len)) | |
(define to-calc (min extras (raindeer-tm rndr))) | |
(define extra-dist (* (raindeer-spd rndr) to-calc)) | |
(if return-name? | |
(cons (+ dist-full-cycles extra-dist) | |
rndr) | |
(+ dist-full-cycles extra-dist))) | |
(define (part1) | |
(~>> | |
(get-input) | |
(map parse-line) | |
(map (lambda (x) (position-after x 2503))) | |
(apply max))) | |
(define (score-after xs tm) | |
(define (increment-scores xs hsh) | |
(if (empty? xs) | |
hsh | |
(increment-scores (cdr xs) | |
(hash-update hsh (car xs) (curry + 1) 0)))) | |
(define (update-score rndrs tm hsh) | |
(define scores (~>> rndrs | |
(map (lambda (x) (position-after x tm #t))) | |
(sort _ #:key car >))) | |
(define leads (~>> scores | |
(filter (lambda (x) (= (car x) (caar scores)))) | |
(map cdr) | |
(map raindeer-nm))) | |
(increment-scores leads hsh)) | |
(define (score-rec xs tm score cur) | |
(if (> cur tm) | |
score | |
(score-rec xs tm (update-score xs cur score) (+ 1 cur)))) | |
(score-rec xs tm #hash() 1)) | |
(define (highest-score hsh) | |
(apply max (hash-values hsh))) | |
(define (part2) | |
(~>> | |
(get-input) | |
(map parse-line) | |
(score-after _ 2503) | |
(highest-score))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment