diff options
author | HJ <thechairman@thechairman.info> | 2021-12-14 09:12:36 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2021-12-14 09:12:36 -0500 |
commit | aa37ee117142819e47cb7099c877ba16a5cc241f (patch) | |
tree | 1e01d75fb36b91a3afe31354faa2ce4ff21bf554 /2021 | |
parent | 9314581a5576bd7cf8a73ea09b2fe6e159d67de1 (diff) | |
download | gleam_aoc-aa37ee117142819e47cb7099c877ba16a5cc241f.tar.gz gleam_aoc-aa37ee117142819e47cb7099c877ba16a5cc241f.zip |
day 14 done
Diffstat (limited to '2021')
-rw-r--r-- | 2021/day-14/day-14.rkt | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/2021/day-14/day-14.rkt b/2021/day-14/day-14.rkt new file mode 100644 index 0000000..d37786e --- /dev/null +++ b/2021/day-14/day-14.rkt @@ -0,0 +1,69 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading + memoize + algorithms + awesome-list) + +(define data (port->lines (open-day 14 2021))) + +(define (starting-polymer d) + (~> d + first + string->list + (sliding 2 1))) + +(define (first-char d) + (first (first (starting-polymer d)))) + +(define (starting-counts d) + (~> d + first + frequencies + hash->list + make-hash)) + +(define (starting-pairs d) + (~>> d + (drop _ 2) + (map (λ~> (substring 0 2) string->list)))) + +(define (new-pairs d) + (~>> d + (drop _ 2) + (map (λ~> (string-replace " -> " "") + string->list + ((match-lambda [(list a b c) (list a c b)]) _) + (sliding 2 1))))) + +(define (transform d) + (~>> (map list (starting-pairs d) (new-pairs d)) + (append*) + (apply hash))) + +(define transformation (transform data)) + +(define/memo (get-count polymer times) + (match times + [0 (for/fold ([counts (hash)]) + ([pair (in-list polymer)]) + (hash-update counts (second pair) add1 0))] + [_ (for*/fold ([counts (hash)]) + ([pair (in-list polymer)] + [(c n) (in-hash (get-count (hash-ref transformation pair) (sub1 times)))]) + (hash-update counts c (λ~> (+ n)) 0))])) + +;; part 1 +(define (process-polymer d n) + (~> d + starting-polymer + (get-count _ n) + (hash-update _ (first-char d) add1 0) + hash-values + (sort >) + ((λ (l) (- (first l) (last l)))))) + +(process-polymer data 10) + +;; part 2 +(process-polymer data 40)
\ No newline at end of file |