aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-20 14:30:15 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-20 14:30:15 -0500
commitf8c7d4e47f04fa2bfbbe5a9487f765bdf898c3ba (patch)
tree8bb7fdfbfbc6506bbb90ee89c7bb0025758104c4 /2022
parenta656a85c43c3817a70368b85f63c106b78b5cf82 (diff)
downloadgleam_aoc-f8c7d4e47f04fa2bfbbe5a9487f765bdf898c3ba.tar.gz
gleam_aoc-f8c7d4e47f04fa2bfbbe5a9487f765bdf898c3ba.zip
day 20 cleanup
Diffstat (limited to '2022')
-rw-r--r--2022/day-20/day-20.rkt24
1 files changed, 13 insertions, 11 deletions
diff --git a/2022/day-20/day-20.rkt b/2022/day-20/day-20.rkt
index 329a372..6dd1070 100644
--- a/2022/day-20/day-20.rkt
+++ b/2022/day-20/day-20.rkt
@@ -1,5 +1,6 @@
#lang racket
-(require advent-of-code)
+(require advent-of-code
+ threading)
(define data (port->list read (open-aoc-input (find-session) 2022 20 #:cache #true)))
@@ -25,22 +26,23 @@
(define (mix-gps data original)
(for/fold ([pts data]) ([pt original])
(mix pt pts)))
+
(define (cycle-mixed-gps mixed)
- (in-sequences (drop mixed (index-of mixed 0)) (in-cycle mixed)))
-(define (calculate-answer mix)
- (for/sum ([id '(1000 2000 3000)]) (sequence-ref mix id)))
+ (define lst (map cdr mixed))
+ (in-sequences (drop lst (index-of lst 0)) (in-cycle lst)))
+
+(define (calculate-answer seq)
+ (for/sum ([id '(1000 2000 3000)]) (sequence-ref seq id)))
;; part 1
-(define part1-mix (cycle-mixed-gps (map cdr (mix-gps gps-indexed gps-indexed))))
-(calculate-answer part1-mix)
+(~> gps-indexed (mix-gps _ gps-indexed) cycle-mixed-gps calculate-answer)
;; part 2
(define encrypted-gps-indexed
(for/list ([pt (in-list gps-indexed)] #:do [(match-define (cons i v) pt)])
(cons i (* 811589153 v))))
-(define part2-mix
- (cycle-mixed-gps (map cdr
- (for/fold ([pts encrypted-gps-indexed]) ([_ (in-range 10)])
- (mix-gps pts encrypted-gps-indexed)))))
-(calculate-answer part2-mix)
+(~>> encrypted-gps-indexed
+ ((λ (data) (foldr (λ (_ pts) (mix-gps pts data)) data (range 10))))
+ cycle-mixed-gps
+ calculate-answer)