adventofcode

Casey 2022-12-09T14:41:38.630289Z

Anyone know if there's an easy way to pair clerk and quil for simple web based visualizations?

Casey 2022-12-14T16:17:00.651199Z

> I think Quil has been lagging way behind on jvm versions… it only works on jdk8 etc as far as I know Oh that's sad.. I haven't used it in quite a few years. But I thought it might be fun to use with this years AOC since there are some cool animated visualizations one can do. And since I've been using Clerk notebooks for AoC I thought it would be cool to pair them. I've also never used Quil in a web browser context, so not sure how that would work

Sam Ritchie 2022-12-14T17:02:49.725659Z

Actually!! That might be totally fine since the cljs side uses p5.js which is up to date

nbardiuk 2022-12-09T15:13:47.935069Z

maybe people in #clerk can help. I guess there should be a way to assign custom visualiser for data based on its metadata, but I haven't looked into it.

Sam Ritchie 2022-12-09T22:12:15.710719Z

I think Quil has been lagging way behind on jvm versions… it only works on jdk8 etc as far as I know

Sam Ritchie 2022-12-09T22:12:48.308039Z

but if you are interested in Mathbox I can get you set up with Clerk and mathbox! https://mathbox.mentat.org/

Sam Ritchie 2022-12-09T22:13:05.516299Z

mathbox examples: https://mathbox.org/

nooga 2022-12-09T21:01:08.759949Z

wut

mbjarland 2022-12-10T14:12:07.436939Z

I asked chatgpt to opimtize some clojure core functions…came back with some interesting reasoning

genmeblog 2022-12-09T21:11:08.053109Z

Is this your code? Now you know the truth...

nooga 2022-12-09T21:13:00.944959Z

yes, this is my code from day 2

Apple 2022-12-09T21:16:35.852629Z

ChatGPT likes you! I ask it what's the date today and all I get is 'no data', 'trained till 2021'

kenj 2022-12-09T21:28:47.607069Z

I wonder if changing "day2" to something else would be enough to throw it off

Marcelo Fernandes 2022-12-09T21:50:32.951829Z

I pasted the solution for my day 5 and asked for it to explain in human terms what it was doing. Not only did it figure out that it was an Advent of Code Day 5 solution (good namespace?) but also explained what the code was doing using words such as warehouse that weren't present in the solution. https://github.com/marceloschreiber/advent-of-code-clojure/tree/main/src/aoc/2022/day_05 (kind of hard to read)

nooga 2022-12-09T23:28:13.863229Z

all that while it stopped learning in 2021

nooga 2022-12-09T23:28:26.059209Z

I fear for my job in 5 years

nooga 2022-12-09T23:28:48.576799Z

not now, but 5 years is enough, maybe we should all jump on the AI train πŸ˜‚

Joe Dumont 2022-12-09T05:01:47.383039Z

I'm learning so much from all your solutions! Thanks for posting everyone. A quick question from a day8 function I saw:

(defn visible?
  [[tree & other-trees]]
  (every? #(< % tree) other-trees))
This captures edges too without needing a single arity version, e.g. (visible? [3]) ;;=> true but it's unclear to me what is happening with every? and the comparison when there's no other value to compare with. It's clearly not equivalent to (< (rest [3]) 3) .

Joe Dumont 2022-12-09T08:17:49.301419Z

Thanks for the response. I understand other-trees will be nil, but I don't understand how it doesn't reduce to the comparison (< nil 3) and a NullPointerException, and what every? does with it.

pez 2022-12-09T08:19:23.770349Z

Check out https://clojuredocs.org/clojure.core/every_q. In fact, I added a think about nil there just a few days ago, because it surprised me too.

Joe Dumont 2022-12-09T08:21:13.001399Z

Aha! "vacuous truth"

pez 2022-12-09T08:23:35.534109Z

Yeah, ChatGPT warns about it too πŸ˜ƒ

πŸ™ƒ 1
pez 2022-12-09T08:33:09.975439Z

ChatGPT's mix of things that makes sense and pure bullshit is interesting!

erwinrooijakkers 2022-12-09T17:09:24.983469Z

pez 2022-12-09T07:11:22.312879Z

other-trees will be nil when there are no args to destruct there. A way to investigate things like this is to use ”inline def”.

(defn visible?
  [[tree & other-trees]]
  (def tree tree)
  (def other-trees other-trees)
  (every? #(< % tree) other-trees))
Then call the function:
(visible? [3])
Then evaluate other-trees in the REPL.

pez 2022-12-09T06:55:53.080019Z

The hypocrisy of i using eval by other names, such as read-string, is going on to this day, though.

πŸ˜† 8
nooga 2022-12-09T10:47:06.263099Z

hehe, I don't even have eval in my implementation

nooga 2022-12-09T10:47:28.025739Z

not because it's hard to make, it's actually trivial, I just don't want to include it

pez 2022-12-09T10:52:12.266829Z

No free will in your kingdom, @xnooga? πŸ˜ƒ

πŸ˜† 1
nooga 2022-12-09T10:53:00.862379Z

it's an edn

πŸ˜‚ 2
pez 2022-12-09T11:04:39.099399Z

Didn't see that one coming!

😁 1
nooga 2022-12-09T14:59:39.481229Z

> toiling in obscurity is what you do in Java land apparently πŸ˜‚

jumar 2022-12-09T07:51:50.547889Z

Quite good except the slow performance of Java land

Andrew Byala 2022-12-09T07:19:55.443139Z

Day 9 - Solutions

genmeblog 2022-12-11T18:20:57.723409Z

Also visualized how to knots travel. First knot and ninth knot (blues are knots from 1-8)

πŸ‘ 1
genmeblog 2022-12-11T18:50:12.800049Z

just for fun, rope with 100 knots πŸ™‚

πŸ™Œ 1
nbardiuk 2022-12-11T19:42:20.017569Z

this visualization reminds me that people come up with part 3: what's the smallest number of knots such that the tail knot doesn't move https://www.reddit.com/r/adventofcode/comments/zgre9m/2022_day_9_bonus_mini_problem_only_try_after_part/

πŸ˜„ 1
genmeblog 2022-12-11T19:59:52.694269Z

426 in my case...

❀️ 4
Max 2022-12-10T14:37:51.885319Z

I thought this ended up being rather pretty

πŸ‘ 3
peterh 2022-12-17T19:35:40.246929Z

Omg, reductions would have made my life so much easier (it seems like I am not the only one, looking at this thread). Also, I really like the approach of using map for simple vector math that I have seen in some solutions. I also have to remember to make more clever use of default values in collection getters.

mbjarland 2022-12-16T10:11:40.295879Z

very late to the game but finally got around to finishing day 09:

(ns aoc.day-09)

(let [ds    {\L [0 -1] \R [0 1] \U [-1 0] \D [1 0]}
      parse (fn [a [d n]] (concat a (repeat (parse-long n) (ds (first d)))))
      data  (reduce parse [] (partition 2 (re-seq #"\w+" (slurp "data/2022_09"))))
      head  (reductions #(mapv + %1 %2) [0 0] data)
      rope  (fn [[y x :as t] h]
              (let [[dy dx] (map - h t)
                    tx {0 0 2 1 -2 -1}]
                [(+ y (tx dy ({2 dy} (abs dx) 0))) (+ x (tx dx ({2 dx} (abs dy) 0)))]))
      tail  #(reductions rope [0 0] %)]
  (prn {:one (count (distinct (tail head)))
        :two (count (distinct (nth (iterate #(tail %) head) 9)))}))

πŸ‘ 3
peterh 2022-12-17T01:48:53.902239Z

Same here. I also made a visualization, mainly to verify my solution and because I want to get used to writing SVG. However, ran into stack overflow with the actual data, so I guess canvas would have been a better choice here. πŸ™‚ https://github.clerk.garden/formsandlines/aoc2022-clojure/commit/a068cdff5c85be281f7bf550313d5e9a2b05d573/src/advent_of_clerk/day_09.html

πŸ‘ 2
πŸ‘πŸ» 1
1
nbardiuk 2022-12-09T08:09:03.203839Z

https://github.com/nbardiuk/adventofcode/blob/master/2022/src/day09.clj

πŸ‘ 1
πŸ‘πŸ» 1
2022-12-09T10:18:33.910599Z

Now contains both parts. https://gist.github.com/maacl/992c94a6000ae1ae206a926eccd34294#file-clj

πŸ™Œ 1
Eugene Huang 2022-12-09T10:29:02.672739Z

πŸ‘‹ new clojurian here. after a rough day 8, am decently pleased with my solution today. happy for any notes! https://github.com/elh/advent-2022/blob/main/src/advent_2022/day_09.clj

πŸ‘ 2
tschady 2022-12-09T10:55:37.327449Z

I reused my grid/moves->path function which takes the input and returns all coords visited, made this trivial. Each subsequent knot treats the earlier knot like the head, so iterate works nicely. Refactored with core.matrix, thanks @benjamin.schwerdtner https://github.com/tschady/advent-of-code/blob/main/src/aoc/2022/d09.clj

3
Casey 2022-12-09T11:16:00.133119Z

More verbose than I would like.. but I'm not golfing after all. https://github.com/Ramblurr/advent-of-code-2022/blob/main/src/aoc/day09.clj

Benjamin 2022-12-09T11:40:28.674889Z

https://github.com/benjamin-asdf/advent-of-code/blob/master/src/Y2022/day9.clj (formatted it) feeling ultra clever today

πŸš€ 1
tschady 2022-12-09T11:45:45.898029Z

@benjamin.schwerdtner nice idea, I’m going to replace some of my vector helpers with core.matrix/{+,-} now.

🀩 1
Felipe 2022-12-09T12:21:12.777899Z

had a total brain freeze for some 10 minutes trying to come up with the generalized step fn reduction https://github.com/FelipeCortez/advent-of-code/blob/master/2022/09.clj

Felipe 2022-12-09T12:30:27.532129Z

@c.oakley108 shouldn't make much of a difference here but last is linear, peek is constant https://github.com/callum-oakley/advent-of-code/blob/b562459233253a8de222e96cc17de2af6e3a040d/src/aoc/2022/09.clj#L28e

Callum Oakley 2022-12-09T12:46:23.342659Z

@felipecortezfi good catch, thanks! cuts the time almost in half 😁 (from 200ms to 100ms)

Felipe 2022-12-09T12:52:06.879439Z

ooo, nice!

Andrew Byala 2022-12-09T15:21:28.367519Z

I like your move-rope implementation, @nbardiuk, since I was reducing over the indexes of my knot vector and calling update for the values in place. I think your reduce with conj of a new vector is a much cleaner solution. Once I update my blog, I think I'll be following suit!

πŸ‘ 1
πŸ‘πŸ» 1
2022-12-09T15:38:20.477939Z

I took a shortcut for part 1 that slowed me down for part 2. In part one since the head only moves up down left or right, the tail, if it moves always takes the old head position. This doesn't work for part two though as intermediate knots might move diagonally.

Aleks 2022-12-09T16:56:23.831099Z

@nbardiuk as usual like your solution especially when it comes to moving something ^_^ btw, just checked that clojure.math has signum.

nbardiuk 2022-12-09T16:58:58.285559Z

@zelark cool! I've never seen it TIL. Will try to integrate into next solutions

Mark Wardle 2022-12-09T18:06:01.082059Z

Yikes. Wasn't sure I liked this today. Bizarrely, my solution to the second part is simpler and more general purpose (configurable numbers of knots) than my solution to the second part! One of those times knowing the second part would have helped! https://github.com/wardle/aoc2022/blob/main/src/day09.clj

Mark Wardle 2022-12-09T18:15:28.215429Z

Think I need to learn 'iterate' given the posted solutions above!

2022-12-09T18:17:47.672489Z

Didn't know the clojure.core.matrix so had to reinvent some wheels, it's not super readable though:

(defn -mapf [fc ac] (mapv #(%1 %2) fc ac))

(defn follow [H T]
  (let [diff (map - H T)]
    (if (> (apply max (map abs diff)) 1)
      (let [fx (map #(cond (> % 0) inc (< % 0) dec :else *) diff)] (-mapf fx T))
      T)))

(defn solve [input len]
  (-> (reduce
       (fn [{p :pos :as g} [m s]]
         (let [go (partial -mapf ((zipmap ["R" "L" "U" "D"] [[inc *] [dec *] [* inc] [* dec]]) m))]
           (loop [p p, n (parse-long s), g g]
             (if (zero? n) (assoc g :pos p)
                 (let [np (reduce #(conj %1 (follow (last %1) %2)) [(go (first p))] (rest p))]
                   (recur np (dec n) (update g :vis #(conj % (last np)))))))))
       {:pos (repeat len [0 0]), :vis #{[0 0]}} input)
      :vis count))

(defn -main [day]
  (let [solution (->> (file->str day) (re-seq #"\w+") (partition 2) (partial solve))]
    {:part1 (solution 2) :part2 (solution 10)}))

nooga 2022-12-09T18:22:06.421959Z

whew, slightly ugly but here it goes:

(ns day9)

(def dirs {"U" [0 -1] "D" [0 1] "R" [1 0] "L" [-1 0]})
(def data (->> "input/day9.txt" slurp lines
               (mapcat #(let [[d n] (split % " ")] (repeat (parse-long n) (dirs d))))
               (reductions #(mapv + %1 %2) [0 0])))

(defn next-pos [[tx ty] [hx hy]]
  (let [sign (fn [x] (cond (zero? x) x (< x 0) -1 :else 1))]
    (if (or (-> tx (- hx) abs (> 1)) (-> ty (- hy) abs (> 1))) 
      [(+ tx (sign (- hx tx))) (+ ty (sign (- hy ty)))]
      [tx ty])))

(let [paths (iterate (partial reductions next-pos) data)]
  (println "1:" (->> paths second (map str) set count))
  (println "2:" (->> (nth paths 9) (map str) set count)))
nailing next-pos took me ages cause I've made a dumb assumption

πŸ‘ 3
2022-12-09T18:23:51.811819Z

Definitely need to learn how reductions work...

πŸ‘€ 1
Mark Wardle 2022-12-09T18:24:27.202579Z

Me too! Great to see other people's solutions for ideas!

nooga 2022-12-09T18:24:30.317829Z

also, my sets don't like vectors so I had to (map str) , it wouldn't be needed in Clojure

bhauman 2022-12-09T18:25:15.652799Z

reductions is like reduce but it returns each step

bhauman 2022-12-09T18:25:44.602489Z

so in the case of the snake of positions you only have to keep the last position in the accumulator

πŸ‘ 1
Mark Wardle 2022-12-09T18:25:51.165529Z

Yes I see - I went through contortions to track each step in my 'reduce' which would have been unnecessary!

2022-12-09T18:26:33.791659Z

Yup, that would make my solution much simpler

bhauman 2022-12-09T18:26:37.440659Z

yeah I get it, it took me a minute to see the mapping to reductions

Mark Wardle 2022-12-09T18:28:22.024669Z

I feel I would be cheating now to change it, but will keep in mind for coming days. That said, I am not sure I can look at my day 9 code and not change it... ! πŸ˜‚

bhauman 2022-12-09T18:32:36.592589Z

Hey if it’s in git, everyone can see the history….. so change away. That’s what I do anyway. It’s not that often that you can have this much fun coding, so I say maximize the fun and experimenting.

πŸ‘ 1
nooga 2022-12-09T18:32:40.962079Z

you know what helps? implementing the core functions as you solve

bhauman 2022-12-09T18:32:47.585809Z

LOL

nooga 2022-12-09T18:32:48.958919Z

you learn a ton this way

bhauman 2022-12-09T18:33:00.854839Z

yeah I bet

😏 1
wevrem 2022-12-09T18:35:50.791319Z

I came up with my own solution this morning, but looking over others I like some of the simpler approaches (i.e. reductions instead of loop/recur). Maybe I’ll go back and re-factor, but good enough for now. https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2022/day_09_rope_snake.clj

tschady 2022-12-09T19:45:49.813469Z

An advantage to iterate/reductions is you have all this step-wise data for the visualizations that you’ll never make

πŸ˜‚ 3
Mark Wardle 2022-12-09T20:00:39.082359Z

Right... this is very much simpler - TIL reductions is a super power. Updated code, still can be improved but good enough. https://github.com/wardle/aoc2022/blob/main/src/day09.clj

leinfink 2022-12-09T20:07:27.421179Z

Haha, I had just discovered reductions today also! https://github.com/leinfink/aoc-2022-clj/blob/main/src/aoc22/day9.clj

πŸ‘ 1
leinfink 2022-12-09T20:09:46.510039Z

Nearly got crazy until I realised it had \d instead of \d+ in my regex so the latter parts of the input failed to parse correctly...

leinfink 2022-12-09T20:12:40.185809Z

Reading through here, I wonder why it didn't occur to me to use reductions for the tail itself, and not just for the intermediate knots, as I did? 🀨 Oh well, happy with my result πŸ˜‡

R.A. Porter 2022-12-09T22:18:29.245159Z

Blergh. What an ugly solution I’ve written today. https://coyotesqrl.github.io/advent-of-code/2022/src/coyotesqrl/2022/day09.html

Apple 2022-12-09T22:21:28.987059Z

check if they are touching/overlap/neighbor

neighbor? (fn [xy1 xy2] (every? #{-1, 0, 1} (map - xy1 xy2)))
catch up to previous knot
(->> (map - prev curr)
     (map #({2 1, -2 -1} % 0))
     (map - prev))

πŸ‘ 7
πŸ‘πŸ» 1
carnundotcom 2022-12-09T22:47:04.556459Z

Happy Friday yous lot :)) I had fun taking https://github.com/mikera/core.matrix for a spin, for the first time! https://github.com/CarnunMP/Advent-of-Code/blob/master/src/y2022/d9.clj

nooga 2022-12-09T23:30:34.935139Z

https://clojurians.slack.com/archives/C0GLTDB2T/p1670624488987059?thread_ts=1670570395.443139&amp;cid=C0GLTDB2T oh that could clean up my next-pos nicely, thanks Apple

Aleks 2022-12-10T07:13:45.931639Z

@zengxh what a nice and neat solution

Andrew Byala 2022-12-09T07:20:44.114919Z

Am I the only fool staying up late to code this and document it? Oh well, so be it! Fun puzzle today. πŸ™‚ β€’ https://github.com/abyala/advent-2022-clojure/blob/main/docs/day09.md β€’ https://github.com/abyala/advent-2022-clojure/blob/main/src/advent_2022_clojure/day09.clj

πŸ‘ 1
Callum Oakley 2022-12-09T07:33:08.288249Z

https://github.com/callum-oakley/advent-of-code/blob/main/src/aoc/2022/09.clj early rather than late for me! agreed this was a fun one. getting more interesting now

2022-12-09T07:39:33.567609Z

https://gitlab.com/maximoburrito/advent2022/-/blob/main/src/day09/main.clj No - I stayed up too. I completely misunderstood the tail following head algorithm, but what I did worked for all sample inputs and the full input on part1 but not on part 2. After re-reading for the 47th time I finally figured out what I was doing wrong. uggghh.... I applies zero cleanup here, so it's a total mess