adventofcode 2021-12-14

Inspired by @sam.h.adams I made my own blog page explaining my solutions, built into AsciiDoc from source. I tag interesting comments and code from the source and only pull those in. After I cleanup the generation script Iโ€™ll add a section explaining it https://github.com/tschady/advent-of-code/blob/main/doc/2021.adoc

๐Ÿ‘ 4

๐ŸงตDay 14 answers thread: post your answers here

๐Ÿ‘ 6
๐Ÿ‘๐Ÿป 1

https://gitlab.com/maximoburrito/advent2021/-/blob/main/src/day14/main.clj so ugly. I hope my eyes don't bleed when I try to read this in the morning

๐Ÿ‘ 3
Miฤทelis Vindavs 2021-12-14T07:19:43.108700Z

Btw,

(->> foo
     ((juxt #(apply max (vals %)) #(apply min (vals %)))))
can be replaced with
(->> (vals foo)
     (apply (juxt max min))

๐Ÿ’ก 4
Miฤทelis Vindavs 2021-12-14T07:25:09.109400Z

Thatโ€™s a super clean and short solution @karlis

1

I had a LOT of issues with this one! Thanks to the people on here who helped, love you guys!

(ns stuartstein777.2021.day14
  (:require [clojure.string :as str]))

(defn parser [line]
  {(subs line 0 2) [(str (subs line 0 1) (subs line 6))
                    (str (subs line 6) (subs line 1 2))]})

(defn solve [a b m]
  (->
   (reduce-kv (fn [acc [k1 k2] v]
                (as-> acc o
                  (merge-with + o {k1 v})
                  (merge-with + o {k2 v}))) {} m)
   (update a inc)
   (update b inc)))

(defn react [reactions m]
  (reduce (fn [acc [k v]]
            (let [[r1 r2] (reactions k)]
              (-> acc
                  (update r1 (fnil + 0) v)
                  (update r2 (fnil + 0) v)))) {} m))


(let [input            (->> (slurp "puzzle-inputs/2021/day14")
                            (str/split-lines))
      polymer-template (first input)
      reactions        (->> input
                            (drop 2)
                            (map parser)
                            (apply merge))
      reactors         (->> (partial react reactions)
                            (repeat 40)
                            (apply comp))
      final-pairs      (->> (partition 2 1 polymer-template)
                            (map (partial apply str))
                            (frequencies)
                            (reactors))
      final-vals       (->> final-pairs
                            (solve (first polymer-template) (last polymer-template))
                            (vals)
                            (map #(/ % 2)))
      max-c            (apply max final-vals)
      min-c            (apply min final-vals)]
  (- max-c min-c))
I was getting caught out by the fact there are duplicate pairs in the real data, but not in test data!

I've spend too much time figuring out what to memoize https://github.com/nbardiuk/adventofcode/blob/master/2021/src/day14.clj

๐Ÿ™Œ 2

Looking at @karlis and @zelark solutions โ€” why donโ€™t you need to bump the count for (first template) in addition to (last template)?

Oh but in this case, I could have done one order better by using a multiply instead of memoization's many adds. I didn't use the fact that counting squashes a dimension to its fullest advantage

@sam.h.adams a good question, bc we get only the first letter from the pairs. By doing it you miss only the last letter in the the last pair. Iโ€™ll give you an example of it. Original template: NNCB. After split into pairs it looks like

NN 1
NC 1
CB 1
After replacing:
NC 1
CN 1
NB 1
BC 1
HB 1
Now if we want to count freqs, we need to take only first one from each pair plus the second one from the last pair because we miss it during reduce.

โž• 1
1

I have a solution

But I don't understand why it works :D

I just noticed that I was getting roughly double the answer

Ah I got it now!

Double counting all letters except the ones at the beginning or the end

@zelark I see now, thanks for the explanation ๐Ÿ™‚

same idea as many of yours, just with some uglier ends ๐Ÿ™‚

I went too far with threading :D

๐Ÿคฏ 1

in the end I made my life harder creating the new pairs beforehand

https://github.com/kfirmanty/advent-of-code-2021/blob/main/src/day14.clj must admit - took me longer than it should have. the first part was a breeze but the second one not so much ๐Ÿ˜„ I should have remembered a lesson from day6 to calculate final solution as I go through iterations instead of trying to reduce it at the end. At the beginning though that there might be some formula to growth or maybe substrings repeat but nope, just wasted time ๐Ÿ˜„

for a lot of these, the hint is โ€œOK, the final answer only wants counts, not the whole string, so what donโ€™t we need to track?โ€

๐Ÿ‘ 1
๐Ÿ’ฏ 1

Advent of iterate , Advent of frequencies

๐Ÿ˜† 8

With a reduce and group-by new year.

@tws nice! I like the succinct approach, it differentiates your writeups from the source itself. Cool use of var metadata too.

1

nicely done!

got my first clojure monkeypatch in there too:

(in-ns 'marginalia.parser)
(defn strip-docstring [_ raw] raw)
(in-ns 'blogify)

๐Ÿฆพ 1

clojure is powerful!