adventofcode

tschady 2021-12-14T02:21:31.104700Z

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
Aleks 2021-12-14T04:55:18.106300Z

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

Fredrik 2021-12-14T05:56:26.106800Z

๐Ÿ‘ 6
๐Ÿ‘๐Ÿป 1
2021-12-14T06:32:01.108Z

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
2021-12-15T22:26:18.157Z

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!

nbardiuk 2021-12-14T09:41:36.111600Z

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

๐Ÿ™Œ 2
Joe 2021-12-14T14:34:57.115400Z

More fun with matrices, so my part 2 is fast for once: Edit: Relatively fast ๐Ÿ˜“ https://github.com/RedPenguin101/aoc2021/blob/main/clojure/src/aoc2021/day14.clj https://github.com/RedPenguin101/aoc2021/blob/main/day14.md

kevinc 2021-12-14T17:59:00.117300Z

https://github.com/kconner/advent-of-code/blob/master/2021/14a.clj,ย https://github.com/kconner/advent-of-code/blob/master/2021/14b.clj. Big computations over a narrow domain are ideal for memoization!

Sam Adams 2021-12-14T18:08:09.118Z

on the verbose side: https://samadams.dev/2021/12/14/advent-of-code-day-14.html

Sam Adams 2021-12-14T18:09:01.118300Z

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

kevinc 2021-12-14T18:13:23.119100Z

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

Aleks 2021-12-14T18:19:47.119300Z

@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
Antonio Bibiano 2021-12-14T20:05:02.120200Z

I have a solution

Antonio Bibiano 2021-12-14T20:05:20.120700Z

But I don't understand why it works :D

Antonio Bibiano 2021-12-14T20:05:55.121600Z

I just noticed that I was getting roughly double the answer

Antonio Bibiano 2021-12-14T20:16:06.122Z

Ah I got it now!

Antonio Bibiano 2021-12-14T20:19:10.122600Z

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

Sam Adams 2021-12-14T20:29:31.122900Z

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

euccastro 2021-12-14T22:13:36.125100Z

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

Antonio Bibiano 2021-12-14T22:15:55.125300Z

I went too far with threading :D

๐Ÿคฏ 1
Antonio Bibiano 2021-12-14T22:18:03.125800Z

Antonio Bibiano 2021-12-14T22:23:23.126200Z

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

2021-12-14T22:39:41.126700Z

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 ๐Ÿ˜„

tschady 2021-12-14T22:53:00.127Z

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
tschady 2021-12-14T13:44:45.114500Z

Advent of iterate , Advent of frequencies

๐Ÿ˜† 8
Marc O'Morain 2021-12-14T14:04:55.115200Z

With a reduce and group-by new year.

Sam Adams 2021-12-14T17:52:40.117100Z

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

1
erwinrooijakkers 2021-12-15T10:15:10.130400Z

nicely done!

tschady 2021-12-14T18:02:16.117800Z

got my first clojure monkeypatch in there too:

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

๐Ÿฆพ 1
Sam Adams 2021-12-14T18:12:14.118900Z

clojure is powerful!