diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /aoc2021/day-14/day-14.rkt | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'aoc2021/day-14/day-14.rkt')
-rw-r--r-- | aoc2021/day-14/day-14.rkt | 61 |
1 files changed, 0 insertions, 61 deletions
diff --git a/aoc2021/day-14/day-14.rkt b/aoc2021/day-14/day-14.rkt deleted file mode 100644 index e445694..0000000 --- a/aoc2021/day-14/day-14.rkt +++ /dev/null @@ -1,61 +0,0 @@ -#lang racket -(require "../../jj-aoc.rkt" - threading - memoize - algorithms - list-utils) - -(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) |