diff options
author | HJ <thechairman@thechairman.info> | 2021-12-21 10:56:50 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2021-12-21 10:56:50 -0500 |
commit | 2c400df353f83c46b61b723c97b5f92b24a45353 (patch) | |
tree | f3313889d90c78f7f28f6a742af5a77cd6a49221 /2021 | |
parent | d17100c0f20a5a17d4b0e98b426aad8fab58b5a8 (diff) | |
download | gleam_aoc-2c400df353f83c46b61b723c97b5f92b24a45353.tar.gz gleam_aoc-2c400df353f83c46b61b723c97b5f92b24a45353.zip |
day 21 cleanup
Diffstat (limited to '2021')
-rw-r--r-- | 2021/day-21/day-21.rkt | 52 |
1 files changed, 19 insertions, 33 deletions
diff --git a/2021/day-21/day-21.rkt b/2021/day-21/day-21.rkt index d2e17b6..46bbbe2 100644 --- a/2021/day-21/day-21.rkt +++ b/2021/day-21/day-21.rkt @@ -19,27 +19,21 @@ [last-turn 0] #:result (list (min player-1-score player-2-score) (* 3 last-turn))) ([turn-count (in-naturals 1)] - [turn current-turn] + [turn-for current-turn] #:break (or (player-1-score . >= . 1000) (player-2-score . >= . 1000))) (define rolls (apply + (stream->list (stream-take dice 3)))) - (cond - [(equal? turn 'player-1) - (values (+ player-1-score - (stream-first (stream-tail player-1-track rolls))) - (stream-tail player-1-track rolls) - player-2-score - player-2-track - (stream-tail dice 3) - turn-count)] - [(equal? turn 'player-2) - (values player-1-score - player-1-track - (+ player-2-score - (stream-first (stream-tail player-2-track rolls))) - (stream-tail player-2-track rolls) - (stream-tail dice 3) - turn-count)])) + (match turn-for + ['player-1 (define next-track (stream-tail player-1-track rolls)) + (values (+ player-1-score (stream-first next-track)) next-track + player-2-score player-2-track + (stream-tail dice 3) + turn-count)] + ['player-2 (define next-track (stream-tail player-2-track rolls)) + (values player-1-score player-1-track + (+ player-2-score (stream-first next-track)) next-track + (stream-tail dice 3) + turn-count)])) (apply * _)) ;; part 2 @@ -52,20 +46,12 @@ [(p1-score . >= . 21) '(1 0)] [(p2-score . >= . 21) '(0 1)] [else - (for/fold ([p-wins '(0 0)]) + (for/fold ([wins '(0 0)]) ([roll (in-list roll-space)]) - (define move-to (~> p1-start - (+ roll) - sub1 - (modulo 10) - add1)) - (match-define (list p1-wins p2-wins) p-wins) - (match-define (list p2-possible-win p1-possible-win) - (next-turns p2-score - (+ p1-score move-to) - p2-start - move-to)) - (list (+ p1-wins p1-possible-win) - (+ p2-wins p2-possible-win)))])) + (define move-to (add1 (modulo (sub1 (+ roll p1-start)) 10))) + (define possible-wins (next-turns p2-score (+ p1-score move-to) p2-start move-to)) + (list (+ (first wins) (second possible-wins)) + (+ (second wins) (first possible-wins ))))])) -(~>> (next-turns 0 0 player-1-start player-2-start) (apply max))
\ No newline at end of file +(~>> (next-turns 0 0 player-1-start player-2-start) + (apply max))
\ No newline at end of file |