aboutsummaryrefslogtreecommitdiff
path: root/2021
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2021-12-21 10:42:02 -0500
committerHJ <thechairman@thechairman.info>2021-12-21 10:42:02 -0500
commitd17100c0f20a5a17d4b0e98b426aad8fab58b5a8 (patch)
tree004bddbbaa8793a9aff6f80d11c439f7098e4d5e /2021
parentf6e6ad98c20e420eebba2c006d332e8db0726af7 (diff)
downloadgleam_aoc-d17100c0f20a5a17d4b0e98b426aad8fab58b5a8.tar.gz
gleam_aoc-d17100c0f20a5a17d4b0e98b426aad8fab58b5a8.zip
day 21 complete
Diffstat (limited to '2021')
-rw-r--r--2021/day-21/day-21.rkt50
1 files changed, 39 insertions, 11 deletions
diff --git a/2021/day-21/day-21.rkt b/2021/day-21/day-21.rkt
index 30a94d2..d2e17b6 100644
--- a/2021/day-21/day-21.rkt
+++ b/2021/day-21/day-21.rkt
@@ -1,6 +1,6 @@
#lang racket
-(require "../../jj-aoc.rkt"
- threading)
+(require threading
+ memoize)
;; not going to bother importing the data since it's just two lines of text
(define player-1-start 4)
@@ -12,9 +12,9 @@
;; part 1
(~> (for/fold ([player-1-score 0]
- [player-1-space (stream-tail track (sub1 player-1-start))]
+ [player-1-track (stream-tail track (sub1 player-1-start))]
[player-2-score 0]
- [player-2-space (stream-tail track (sub1 player-2-start))]
+ [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)))
@@ -26,18 +26,46 @@
(cond
[(equal? turn 'player-1)
(values (+ player-1-score
- (stream-first (stream-tail player-1-space rolls)))
- (stream-tail player-1-space rolls)
+ (stream-first (stream-tail player-1-track rolls)))
+ (stream-tail player-1-track rolls)
player-2-score
- player-2-space
+ player-2-track
(stream-tail dice 3)
turn-count)]
[(equal? turn 'player-2)
(values player-1-score
- player-1-space
+ player-1-track
(+ player-2-score
- (stream-first (stream-tail player-2-space rolls)))
- (stream-tail player-2-space rolls)
+ (stream-first (stream-tail player-2-track rolls)))
+ (stream-tail player-2-track rolls)
(stream-tail dice 3)
turn-count)]))
- (apply * _)) \ No newline at end of file
+ (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 ([p-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)))]))
+
+(~>> (next-turns 0 0 player-1-start player-2-start) (apply max)) \ No newline at end of file