aboutsummaryrefslogtreecommitdiff
path: root/2021/day-14
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-11-26 01:43:33 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-11-26 01:43:33 -0500
commitfeccf3f6f0a806b3317d1f399e3e8b42945c4f09 (patch)
treebf15ce045d1106c1b7f7de30c27540f40d0bf947 /2021/day-14
parent8b624fe7d2751337b1f16830cc9c041df73e99e7 (diff)
downloadgleam_aoc-feccf3f6f0a806b3317d1f399e3e8b42945c4f09.tar.gz
gleam_aoc-feccf3f6f0a806b3317d1f399e3e8b42945c4f09.zip
using raco fmt, replacing missing dependency
Diffstat (limited to '2021/day-14')
-rw-r--r--2021/day-14/day-14.rkt60
1 files changed, 26 insertions, 34 deletions
diff --git a/2021/day-14/day-14.rkt b/2021/day-14/day-14.rkt
index d37786e..e445694 100644
--- a/2021/day-14/day-14.rkt
+++ b/2021/day-14/day-14.rkt
@@ -3,67 +3,59 @@
threading
memoize
algorithms
- awesome-list)
+ list-utils)
(define data (port->lines (open-day 14 2021)))
(define (starting-polymer d)
- (~> d
- first
- string->list
- (sliding 2 1)))
+ (~> 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))
+ (~> d first frequencies hash->list make-hash))
(define (starting-pairs d)
- (~>> d
- (drop _ 2)
- (map (λ~> (substring 0 2) string->list))))
+ (~>> 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)))))
+ 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)))
+ (~>> (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))]))
+ (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))))))
+ 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
+(process-polymer data 40)