aboutsummaryrefslogtreecommitdiff
path: root/2021
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2021-12-21 00:38:07 -0500
committerHJ <thechairman@thechairman.info>2021-12-21 00:38:07 -0500
commitf6e6ad98c20e420eebba2c006d332e8db0726af7 (patch)
treed6dd0a931d8e3516729c07f125fcf11515153301 /2021
parentb6d7ba7e12e618fc4748f8b857dfa11669c0d530 (diff)
downloadgleam_aoc-f6e6ad98c20e420eebba2c006d332e8db0726af7.tar.gz
gleam_aoc-f6e6ad98c20e420eebba2c006d332e8db0726af7.zip
day 21 part 1 complete
Diffstat (limited to '2021')
-rw-r--r--2021/day-21/day-21.rkt43
1 files changed, 43 insertions, 0 deletions
diff --git a/2021/day-21/day-21.rkt b/2021/day-21/day-21.rkt
new file mode 100644
index 0000000..30a94d2
--- /dev/null
+++ b/2021/day-21/day-21.rkt
@@ -0,0 +1,43 @@
+#lang racket
+(require "../../jj-aoc.rkt"
+ threading)
+
+;; 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-space (stream-tail track (sub1 player-1-start))]
+ [player-2-score 0]
+ [player-2-space (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 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-space rolls)))
+ (stream-tail player-1-space rolls)
+ player-2-score
+ player-2-space
+ (stream-tail dice 3)
+ turn-count)]
+ [(equal? turn 'player-2)
+ (values player-1-score
+ player-1-space
+ (+ player-2-score
+ (stream-first (stream-tail player-2-space rolls)))
+ (stream-tail player-2-space rolls)
+ (stream-tail dice 3)
+ turn-count)]))
+ (apply * _)) \ No newline at end of file