diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /aoc2021/day-21/day-21.rkt | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'aoc2021/day-21/day-21.rkt')
-rw-r--r-- | aoc2021/day-21/day-21.rkt | 59 |
1 files changed, 0 insertions, 59 deletions
diff --git a/aoc2021/day-21/day-21.rkt b/aoc2021/day-21/day-21.rkt deleted file mode 100644 index 9ca9b1b..0000000 --- a/aoc2021/day-21/day-21.rkt +++ /dev/null @@ -1,59 +0,0 @@ -#lang racket -(require threading - memoize) - -;; not going to bother importing the data since it's just two lines of text -(define player-1-start 4) -(define player-2-start 6) - -(define track (sequence->stream (in-cycle (inclusive-range 1 10)))) -(define current-turn (in-cycle (list 'player-1 'player-2))) -(define die-rolls (sequence->stream (in-cycle (inclusive-range 1 100)))) - -;; part 1 -(~> (for/fold ([player-1-score 0] - [player-1-track (stream-tail track (sub1 player-1-start))] - [player-2-score 0] - [player-2-track (stream-tail track (sub1 player-2-start))] - [dice die-rolls] - [last-turn 0] - #:result (list (min player-1-score player-2-score) (* 3 last-turn))) - ([turn-count (in-naturals 1)] - [turn-for current-turn] - #:break (or (player-1-score . >= . 1000) (player-2-score . >= . 1000))) - (define rolls (apply + (stream->list (stream-take dice 3)))) - (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 -(define d3 (list 1 2 3)) -(define roll-space (~>> (cartesian-product d3 d3 d3) (map (λ~>> (apply +))))) - -(define/memo - (next-turns p1-score p2-score p1-start p2-start) - (cond - [(p1-score . >= . 21) '(1 0)] - [(p2-score . >= . 21) '(0 1)] - [else - (for/fold ([wins '(0 0)]) ([roll (in-list roll-space)]) - (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)) |