This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-14
Channels
- # adventofcode (38)
- # announcements (42)
- # aws (3)
- # babashka (8)
- # beginners (165)
- # calva (36)
- # chlorine-clover (14)
- # cider (6)
- # clj-kondo (5)
- # cljsrn (33)
- # clojure (27)
- # clojure-australia (1)
- # clojure-czech (1)
- # clojure-doc (1)
- # clojure-europe (26)
- # clojure-nl (6)
- # clojure-spec (6)
- # clojure-uk (3)
- # clojurescript (10)
- # code-reviews (20)
- # conjure (1)
- # core-logic (5)
- # cursive (3)
- # data-science (5)
- # datomic (35)
- # emacs (1)
- # figwheel-main (3)
- # fulcro (10)
- # honeysql (1)
- # introduce-yourself (4)
- # jobs (3)
- # jobs-discuss (4)
- # minecraft (2)
- # missionary (28)
- # nextjournal (3)
- # off-topic (45)
- # pathom (7)
- # polylith (1)
- # portal (22)
- # practicalli (2)
- # re-frame (4)
- # reagent (19)
- # releases (3)
- # remote-jobs (3)
- # reveal (1)
- # rum (4)
- # shadow-cljs (37)
- # spacemacs (14)
- # sql (1)
- # tools-build (7)
- # tools-deps (16)
- # vim (13)
- # xtdb (15)
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
🧵Day 14 answers thread: post your answers here
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
Btw,
(->> foo
((juxt #(apply max (vals %)) #(apply min (vals %)))))
can be replaced with
(->> (vals foo)
(apply (juxt max min))
https://github.com/axelarge/advent-of-code/blob/master/src/advent_of_code/y2021/day14.clj
I've spend too much time figuring out what to memoize https://github.com/nbardiuk/adventofcode/blob/master/2021/src/day14.clj
1ms / 4ms. Similar to others. Writeup: https://github.com/tschady/advent-of-code/blob/main/doc/2021.adoc#aoc2021d14 code: https://github.com/tschady/advent-of-code/blob/main/src/aoc/2021/d14.clj
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
and me: https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2021/day14.clj
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!
on the verbose side: https://samadams.dev/2021/12/14/advent-of-code-day-14.html
Looking at @U0CKR20TW and @U067R559Q 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.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
@U067R559Q I see now, thanks for the explanation 🙂
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?”
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!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.
got my first clojure monkeypatch in there too:
(in-ns 'marginalia.parser)
(defn strip-docstring [_ raw] raw)
(in-ns 'blogify)
nicely done!