Day 9 - Solutions
Using @raghavsharma's iterate magic, I revised my Day 9:
https://github.com/rjray/advent-2023-clojure/blob/master/src/advent_of_code/day09bis.clj
Cut the run-times in half.
OK, I'm stumped. My puzzle data has to be corrupt or something. I wrote part 1, the answer I submitted was said to be too high. I've copy-pasted 4-5 different working solutions (in both Clojure and Python) and every one of them give the same answer for my input (the one my code produces). Anyone seen a similar problem?
Is there any chatter on reddit that puzzle data is iffy? It seems very unlikely that would go unnoticed.
None so far. I just posted an explicit question to that effect.
I tried two Python solutions from reddit, and Apple's pastebin, and norman's original. All 4 gave the same answer my code does.
Is this helpful: my input had 200 lines.
Well, Wastl himself answered saying that I had the right answer for my input, I should try entering it again really really carefully. The third time, it worked...
And, thanks to knowing the "reverse" trick, part 2 is already done. I'm really chuffed about part 1, though.
Short and sweet with iterate + take-while and reduce https://github.com/skazhy/advent/blob/master/src/advent/2023/day9.clj
nothing new to offer, except my often used intervals helper:
(defn intervals
"Returns the seq of intervals between each element of `xs`, step `n` (default 1)"
([xs] (intervals 1 xs))
([n xs] (map - (drop n xs) xs)))
https://github.com/tschady/advent-of-code/blob/main/src/aoc/2023/d09.cljThis wasβ¦ much easier than expected π
@alpox his solution https://squint-cljs.github.io/squint/?boilerplate=https%3A%2F%2Fgist.githubusercontent.com%2Fborkdude%2Fcf94b492d948f7f418aa81ba54f428ff%2Fraw%2Fa6e9992b079e20e21d753e8c75a7353c5908b225%2Faoc_ui.cljs&repl=true&src=OzsgSGVscGVyIGZ1bmN0aW9uczoKOzsgKGZldGNoLWlucHV0IHllYXIgZGF5KSAtIGdldCBBT0MgaW5wdXQKOzsgKGFwcGVuZCBzdHIpIC0gYXBwZW5kIHN0ciB0byBET00KOzsgKHNweSB4KSAtIGxvZyB4IHRvIGNvbnNvbGUgYW5kIHJldHVybiB4Cgo7OyBPcmlnaW5hbCBzb2x1dGlvbiBieSBFbGlhcyBCZXJuaGF1dAoKOzsgUmVtZW1iZXIgdG8gdXBkYXRlIHRoZSB5ZWFyIGF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ChmbiBbYWNjIHJvd10gKCsgYWNjIChsYXN0IHJvdykpKSkpCihkZWZuIHBhcnQtMiBbaW5wdXRdIChydW4gaW5wdXQgKGZuIFthY2Mgcm93XSAoLSAoZmlyc3Qgcm93KSBhY2MpKSkpCgooY29tbWVudAogIChwYXJ0LTEgaW5wdXQpCiAgKHBhcnQtMiBpbnB1dCkKICAp
Not a smart solution, but it works : https://github.com/arnaudgeiser/advent-of-code/blob/master/2023/clojure/src/advent_of_code/day9.clj
reverse was a key https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2023/day09.clj
spent a long time looking for a catch. no catch! was kind of proud until I saw the one liners https://github.com/FelipeCortez/advent-of-code/blob/master/2023/09.clj
Easy task, both parts in a same evaluation:
(->> input
str/split-lines
(map (fn [s]
(let [d (read-string (str "[" s "]"))
data (loop [v d
r (list d)]
(let [t (mapv - (rest v) v)]
(if (every? zero? t)
(cons t r)
(recur t (cons t r)))))]
(reduce (fn [{:keys [f l]} v]
{:f (- (first v) f)
:l (+ l (last v))})
{:f 0 :l 0} data))))
(apply merge-with +))Day 9 really rewards recursive thinking https://github.com/bhauman/adv2023/blob/main/src/adv2023/day09/sol.clj
@bhauman amazing. https://squint-cljs.github.io/squint/?boilerplate=https%3A%2F%2Fgist.githubusercontent.com%2Fborkdude%2Fcf94b492d948f7f418aa81ba54f428ff%2Fraw%2Fa6e9992b079e20e21d753e8c75a7353c5908b225%2Faoc_ui.cljs&repl=true&src=OzsgSGVscGVyIGZ1bmN0aW9uczoKOzsgKGZldGNoLWlucHV0IHllYXIgZGF5KSAtIGdldCBBT0MgaW5wdXQKOzsgKGFwcGVuZCBzdHIpIC0gYXBwZW5kIHN0ciB0byBET00KOzsgKHNweSB4KSAtIGxvZyB4IHRvIGNvbnNvbGUgYW5kIHJldHVybiB4Cgo7OyBTb2x1dGlvbiBieSBiaGF1bWFuOgo7OyBodHRwczovL2dpdGh1Yi5jb20vYmhhdW1hbi9hZHYyMDIzL2Jsb2IvbWFpbi9zcmMvYWR2MjAyMy9kYXkwOS9zb2wuY2xqCgooZGVmIGlucHV0CiAgKC0%2BPiAoanMtYXdhaXQgKGZldGNoLWlucHV0IDIwMjMgOSkpCiAgICAoc3RyL3NwbGl0LWxpbmVzKQogICAgKG1hcCAjKGFzLT4gJSB4CiAgICAgICAgICAgIChzdHIgIlsiIHggIl0iKQogICAgICAgICAgICAoc3RyL3JlcGxhY2UgeCAiICIgIiwiKQogICAgICAgICAgICAoanMvSlNPTi5wYXJzZSB4KSkpKSkKCihkZWZuIGRpZmZzIFtsXQogICgtPj4gbCAocGFydGl0aW9uIDIgMSkgKG1hcHYgIyhhcHBseSAtIChyZXZlcnNlICUpKSkpKQoKKGRlZm4gbmV4dC1udW0gW251bS1zZXFdCiAgKGxldCBbZHMgKGRpZmZzIG51bS1zZXEpXQogICAgKGlmIChldmVyeT8gemVybz8gZHMpCiAgICAgIChsYXN0IG51bS1zZXEpCiAgICAgICgrIChsYXN0IG51bS1zZXEpCiAgICAgICAgKG5leHQtbnVtIGRzKSkpKSkKCjs7IHBhcnQgMQooY29tbWVudAogIChyZWR1Y2UgKyAobWFwIG5leHQtbnVtIGlucHV0KSkKICApCgooZGVmbiBwcmV2LW51bSBbbnVtLXNlcV0KICAobGV0IFtkcyAoZGlmZnMgbnVtLXNlcSldCiAgICAoaWYgKGV2ZXJ5PyB6ZXJvPyBkcykKICAgICAgKGZpcnN0IG51bS1zZXEpCiAgICAgICgtIChmaXJzdCBudW0tc2VxKQogICAgICAgIChwcmV2LW51bSBkcykpKSkpCgo7OyBwYXJ0IDIKKGNvbW1lbnQKICAocmVkdWNlICsgKG1hcCBwcmV2LW51bSBpbnB1dCkpCiAgKQ%3D%3D
https://github.com/dgtized/advent-of-code/blob/master/2023/src/day09/mirage_maintenance.clj#L1
OK, now that I've slept and am a little less salty about the input glitching... https://github.com/rjray/advent-2023-clojure/blob/master/src/advent_of_code/day09.clj
@borkdude oh very cool
I really like @erdosβs recursive solve and the diffs method as well.
(defn diffs [coll] (map - (next coll) coll)) Nice!
@bhauman if you want to switch to cherry, just change /squint/ to /cherry/ in the url
@borkdude oh interesting. Whatβs the reload-ability story for squint?
what do you mean reload-ability? as in incremental REPL behavior?
reloading namespaces?
in the playground, if the REPL mode is on (see on the right). you can evaluate form by form using Cmd-Enter
reloading namespaces: I usually rely on vite for hot-reloading with squint front-end projects + squint watch for re-compiling changes files
here is an example of that https://github.com/squint-cljs/squint/tree/main/examples/vite-react
Love me some iterate magix.
Day 9 is like a breath of fresh air: https://github.com/rmrt1n/advent-of-code-2023-clj/blob/main/src/aoc/day09.clj
https://gitlab.com/maximoburrito/advent2023/-/blob/main/src/day09/main.clj
My https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2023/day_09.clj.
https://github.com/erdos/advent-of-code/blob/master/2023/day09.clj figured that a recursive solution is the simplest, although, it can be done with loop+recur and reduce as well.
My https://github.com/wevre/advent-of-code/blob/3193fdd801e4a25fc104ceda5597b0f62ce05dda/notes/notes-aoc2023.txt#L210 for day 09.
a surprisingly straight-forward puzzle for a weekend. I was expecting something much more complicated.
I hadn't considered just using reverse for part 2.
@erdos I like your reduce version.
@michaeljweaver I like your use of transduce!
https://github.com/zelark/AoC/blob/master/src/zelark/aoc_2023/day_09.clj
@michaeljweaver nice solution, it helped me simplified mine ^_^
Iβve learned a lot from you over the years, @zelark, Iβm glad you are posting solutions.
A very, very https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2023/day_09_v2.clj after imbibing those of @erdos and @zelark.
@zelark might I suggest you add a transducer arity to your take-until?
@michaeljweaver sure, actually I copied it from https://groups.google.com/g/clojure-dev/c/NaAuBz6SpkY
Until the last year I strived to not use libraries at all. However, I guess itβd better to pick take-upto from medley now