From eb202d2abcba77780c72c9c3999fba2aa0c0f4f7 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Wed, 15 Dec 2021 20:20:12 -0500 Subject: [PATCH] day 14 --- day14.rkt | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ day14.test.txt | 18 +++++++++ day14.txt | 102 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 day14.rkt create mode 100644 day14.test.txt create mode 100644 day14.txt diff --git a/day14.rkt b/day14.rkt new file mode 100644 index 0000000..5e58fa6 --- /dev/null +++ b/day14.rkt @@ -0,0 +1,104 @@ +#lang racket + +(define input + (let* ([lines (file->lines "day14.txt")] + [template (first lines)] + [rule-strings (drop lines 2)] + [rules (map (λ (rs) (string-split rs " -> ")) rule-strings)]) + (list template rules))) + +(define template (first input)) +(define rules (second input)) + +(define (find-pairs template) + (reverse + (foldl (λ (el acc) (cons (substring template el (+ 2 el)) acc)) + '() + (range 0 (sub1 (string-length template)))))) + +(define (find-inserts pairs rules) + (map (λ (p) (second (assoc p rules))) pairs)) + +(define (insert-pairs template inserts) + (foldl (λ (idx acc) + (let* ([x (string (string-ref template idx))] + [insert (list-ref (append inserts (list "")) idx)]) + (string-append acc x insert))) + "" (range 0 (string-length template)))) + +(define (step template rules) + (let* ([pairs (find-pairs template)] + [inserts (find-inserts pairs rules)] + [next (insert-pairs template inserts)]) + next)) + +(define (element-counts str) + (let* ([lst (map string (string->list str))] + [ht (make-hash)]) + (for-each (λ (x) (hash-set! ht x (+ 1 (hash-ref ht x 0)))) lst) + ht)) + +(define part1 + (let* ([final (foldl (λ (i acc) (step acc rules)) template (range 0 10))] + [counts (element-counts final)] + [values (hash-values counts)] + [minimum (apply min values)] + [maximum (apply max values)] + [diff (- maximum minimum)]) + diff)) + +part1 + +; PART 2 +; uhoh. start over + +(define (inc-hash ht key amount) + (hash-set! ht key (+ amount (hash-ref! ht key 0)))) + +(define (dec-hash ht key amount) + (hash-set! ht key (- (hash-ref! ht key 0) amount))) + +(define (step2 ht) + (define pairs (filter (λ (p) (> (cdr p) 0)) (hash->list ht))) + (for-each (λ (pair-and-count) + (let* ([pair (car pair-and-count)] + [count (cdr pair-and-count)] + [ins (second (assoc pair rules))] + [new1 (string-append (substring pair 0 1) ins)] + [new2 (string-append ins (substring pair 1 2))]) + (inc-hash ht new1 count) + (inc-hash ht new2 count) + (dec-hash ht pair count))) + pairs) + ht) + +(define (pairs->element-counts ht) + (let* ([el-ht (make-hash)] + [pair-counts (hash->list ht)]) + (for-each (λ (p) (let* ([pair (car p)] + [el1 (substring pair 0 1)] + [el2 (substring pair 1 2)] + [count (cdr p)]) + (inc-hash el-ht el1 count) + (inc-hash el-ht el2 count))) + pair-counts) + el-ht)) + +(define part2 + (let* ([ht (make-hash)] + [pairs (find-pairs template)]) ; find the initial pairs in the template + (for-each (λ (pair) (inc-hash ht pair 1)) pairs) ; add them to the hash table + (let* ([res (foldl (λ (el acc) (step2 acc)) ht (range 0 40))] ; step! + [el-counts (pairs->element-counts res)] + [first-char (substring template 0 1)] + [last-char (substring template (sub1 (string-length template)))]) + (inc-hash el-counts first-char 1) ; add the first.. + (inc-hash el-counts last-char 1) ; and last chars twice.. + (let* ([lst (hash-map el-counts (λ (key val) (list key (/ val 2))))] ; then divide everything by 2 + [values (map second lst)] + [minimum (apply min values)] + [maximum (apply max values)] + [diff (- maximum minimum)]) + diff)))) + +part2 \ No newline at end of file diff --git a/day14.test.txt b/day14.test.txt new file mode 100644 index 0000000..b5594dd --- /dev/null +++ b/day14.test.txt @@ -0,0 +1,18 @@ +NNCB + +CH -> B +HH -> N +CB -> H +NH -> C +HB -> C +HC -> B +HN -> C +NN -> C +BH -> H +NC -> B +NB -> B +BN -> B +BB -> N +BC -> B +CC -> N +CN -> C diff --git a/day14.txt b/day14.txt new file mode 100644 index 0000000..56c7184 --- /dev/null +++ b/day14.txt @@ -0,0 +1,102 @@ +BNSOSBBKPCSCPKPOPNNK + +HH -> N +CO -> F +BC -> O +HN -> V +SV -> S +FS -> F +CV -> F +KN -> F +OP -> H +VN -> P +PF -> P +HP -> H +FK -> K +BS -> F +FP -> H +FN -> V +VV -> O +PS -> S +SK -> N +FF -> K +PK -> V +OF -> N +VP -> K +KB -> H +OV -> B +CH -> F +SF -> F +NH -> O +NC -> N +SP -> N +NN -> F +OK -> S +BB -> S +NK -> S +FH -> P +FC -> S +OB -> P +VS -> P +BF -> S +HC -> V +CK -> O +NP -> K +KV -> S +OS -> V +CF -> V +FB -> C +HO -> S +BV -> V +KS -> C +HB -> S +SO -> N +PH -> C +PN -> F +OC -> F +KO -> F +VF -> V +CS -> O +VK -> O +FV -> N +OO -> K +NS -> S +KK -> C +FO -> S +PV -> S +CN -> O +VC -> P +SS -> C +PO -> P +BN -> N +PB -> N +PC -> H +SH -> K +BH -> F +HK -> O +VB -> P +NV -> O +NB -> C +CP -> H +NO -> K +PP -> N +CC -> S +CB -> K +VH -> H +SC -> C +KC -> N +SB -> B +BP -> P +KP -> K +SN -> H +KF -> K +KH -> B +HV -> V +HS -> K +NF -> B +ON -> H +BO -> P +VO -> K +OH -> C +HF -> O +BK -> H