Fork me on GitHub
#adventofcode
<
2021-12-14
>
tschady02:12:31

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
alekszelark04:12:18

🧵Day 14 answers thread: post your answers here

norman06:12:01

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 Vindavs07:12:43

Btw,

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

💡 4
Miķelis Vindavs07:12:09

That’s a super clean and short solution @U0CKR20TW

thanks 1
nbardiuk09:12:36

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

🙌 2
Sam Adams18:12:01

Looking at @U0CKR20TW and @U067R559Q solutions — why don’t you need to bump the count for (first template) in addition to (last template)?

kevinc18:12:23

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

alekszelark18:12:47

@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.

thanks 1
1
Antonio Bibiano20:12:02

I have a solution

Antonio Bibiano20:12:20

But I don't understand why it works :D

Antonio Bibiano20:12:55

I just noticed that I was getting roughly double the answer

Antonio Bibiano20:12:06

Ah I got it now!

Antonio Bibiano20:12:10

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

Sam Adams20:12:31

@U067R559Q I see now, thanks for the explanation 🙂

euccastro22:12:36

same idea as many of yours, just with some uglier ends 🙂

Antonio Bibiano22:12:55

I went too far with threading :D

🤯 1
Antonio Bibiano22:12:23

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

karol22:12:41

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 😄

tschady22:12:00

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
Stuart22:12:18

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!

tschady13:12:45

Advent of iterate , Advent of frequencies

😆 8
Marc O'Morain14:12:55

With a reduce and group-by new year.

Sam Adams17:12:40

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

thanks2 1
tschady18:12:16

got my first clojure monkeypatch in there too:

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

🦾 1
Sam Adams18:12:14

clojure is powerful!