From f6e6ad98c20e420eebba2c006d332e8db0726af7 Mon Sep 17 00:00:00 2001 From: HJ Date: Tue, 21 Dec 2021 00:38:07 -0500 Subject: day 21 part 1 complete --- 2021/day-21/day-21.rkt | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2021/day-21/day-21.rkt (limited to '2021') 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 -- cgit v1.2.3