aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-20 13:24:48 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-20 13:24:48 -0500
commit4e8f40a3e21628f9cd5935a4d7bb0f42297a117f (patch)
tree837f29ee844e6894c4b8d755e177cf55da771276 /2022
parent7d3789f139d34ff33c3e9586b7e7781e3cf23d3e (diff)
downloadgleam_aoc-4e8f40a3e21628f9cd5935a4d7bb0f42297a117f.tar.gz
gleam_aoc-4e8f40a3e21628f9cd5935a4d7bb0f42297a117f.zip
day 20 complete (!)
Diffstat (limited to '2022')
-rw-r--r--2022/day-20/day-20.rkt47
1 files changed, 47 insertions, 0 deletions
diff --git a/2022/day-20/day-20.rkt b/2022/day-20/day-20.rkt
new file mode 100644
index 0000000..8132767
--- /dev/null
+++ b/2022/day-20/day-20.rkt
@@ -0,0 +1,47 @@
+#lang racket
+(require advent-of-code)
+
+;(define data '(1 2 -3 3 -2 0 4))
+(define data (port->list read (open-aoc-input (find-session) 2022 20 #:cache #true)))
+
+(define gps-lst data)
+(define gps-len (length gps-lst))
+(define gps-indexed (map cons (inclusive-range 1 gps-len) gps-lst))
+
+(define (mix pt data)
+ (match-define (list left ... (== pt) right ...) data)
+ (define start (index-of data pt))
+ (define move-by (modulo (cdr pt) (sub1 gps-len)))
+ (cond
+ [(= 0 move-by) data]
+ [(<= move-by (length right))
+ (match-define-values (new-left new-right)
+ (split-at (append left right) (modulo (+ move-by start) (sub1 gps-len))))
+ (append new-left (list pt) new-right)]
+ [else
+ (match-define-values (new-left new-right)
+ (split-at (append left right) (modulo (+ move-by start) (sub1 gps-len))))
+ (append new-left (list pt) new-right)]))
+
+(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)))
+
+;; part 1
+(define part1-mix (cycle-mixed-gps (map cdr (mix-gps gps-indexed gps-indexed))))
+(calculate-answer part1-mix)
+
+;; 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)