adventofcode

Aleks 2020-12-17T12:24:11.350700Z

did it as an exercise, cause I rarely write macros

👍 1
❤️ 1
2020-12-17T12:37:29.351700Z

I did not realize that I could use the multi-arity of = .. thanks !

2020-12-17T12:40:13.351900Z

Hey I like your typeface! The ligature looks so amazing. What’s the typeface you’re using?

2020-12-17T12:40:28.352100Z

it looks like firacode

Aleks 2020-12-17T12:41:05.352300Z

Yeah, it’s FiraCode by @tonsky https://github.com/tonsky/FiraCode

❤️ 2
2020-12-17T12:41:32.352700Z

Thanks!

erwinrooijakkers 2020-12-17T15:01:45.358900Z

Can you copy the code, wondering what it does 🙂

erwinrooijakkers 2020-12-17T15:02:16.359100Z

hard coded vector of [-1, 0 1]

erwinrooijakkers 2020-12-17T15:02:19.359300Z

At compile time?

erwinrooijakkers 2020-12-17T15:02:22.359500Z

Depending on the n?

Aleks 2020-12-17T15:02:51.359700Z

(defmacro adjacent [n]
  (let [ds (repeatedly n #(gensym "d"))
        bindings (mapcat #(-> [% [-1 0 1]]) ds)]
    `(for [~@bindings :when (not= 0 ~@ds)] [~@ds])))

(defn neighbours [adjacent loc]
  (map #(mapv + loc %) adjacent))

(partial neighbours (adjacent 3))

Aleks 2020-12-17T15:04:54.359900Z

it calculates adjacent locations for n-dimension space.

Aleks 2020-12-17T15:05:34.360100Z

You can use it like

(def neighbours-3d (partial neighbours (adjacent 3)))

(neighbours-3d [0 0 0])

2020-12-17T15:10:36.361300Z

Yeah, the for could be replaced by the list of literal of relative neighbor positions at compile time.

Aleks 2020-12-17T16:07:41.367700Z

@vincent.cantin can you show how to to do that?

2020-12-17T16:08:16.367900Z

I am still at work, I will try to do that after.

Aleks 2020-12-17T16:35:55.375100Z

I came up with this

(defmacro adjacent [n]
  (let [ds (repeatedly n #(gensym "d"))
        bindings (mapcat #(-> [% [-1 0 1]]) ds)]
    (eval `(vec (for [~@bindings :when (not= 0 ~@ds)] [~@ds])))))

👌 1
2020-12-17T16:38:16.375400Z

you don’t need the eval, just evaluate the sequence (and put it in a vector) outside of the backtick.

2020-12-17T16:41:07.375600Z

oh .. I see, if you want to use for you can’t have a dynamic n , you will have to use map with recursion … or even better, comp/cartesian-product

2020-12-17T16:42:23.375900Z

well .. it works with eval so why not.

2020-12-17T17:12:19.376200Z

@zelark you can use this to generate the neighbors:

(defn adjacents [dimension]
  (-> (iterate (fn [coords]
                 (mapcat (fn [coord]
                           (map (fn [x]
                                  (conj coord x))
                                [-1 0 1]))
                         coords))
               [[]])
      (nth dimension)
      (->> (remove (fn [coord]
                     (every? #{0} coord))))
      vec))

#_(adjacents 2)

(defmacro neighbors [& coords]
  (let [adj (adjacents (count coords))]
    `(->> ~adj
          (mapv (partial mapv + ~(vec coords))))))

#_(macroexpand-1 '(neighbors x y))
#_(neighbors 5 10)

Aleks 2020-12-17T17:27:15.376500Z

Yeah, it’s also a good option, I saw it in @nbardiuk’s solution. However, I’m happy with two different functions I have. The macro is just for fun.

Aleks 2020-12-17T17:28:36.376900Z

btw, I found (defn ~name ~@pre-args ~args ~(apply (eval (list fn args expr)) args))` in clojure.core.

2020-12-17T17:29:07.377100Z

yeah, eval is ok during compile time

Aleks 2020-12-17T17:35:33.377400Z

also, simple approach like this

(defn neighbours-4d [[x y z w]]
  (for [dx [-1 0 1] dy [-1 0 1] dz [-1 0 1] dw [-1 0 1] :when (not= 0 dx dy dz dw)]
    [(+ x dx) (+ y dy) (+ z dz) (+ w dw)]))
works faster then
(defn neighbours [adjacent loc]
  (map #(mapv + loc %) adjacent))
even with predefined adjacent.

2020-12-17T17:43:40.377700Z

I made another version, for fun:

(defmacro neighbors [& coords]
  (let [dimension (count coords)
        adj (adjacents dimension)
        coord-vars (repeatedly dimension #(gensym "coord"))
        local-vars (repeatedly dimension #(gensym "local"))]
    `(let [~@(interleave coord-vars coords)]
       (mapv (fn [[~@local-vars]]
               [~@(map (fn [lv cv]
                         `(+ ~lv ~cv))
                       local-vars
                       coord-vars)])
             ~adj))))

(neighbors 5 10)

pez 2020-12-17T15:53:54.363900Z

I have lost my steam. Yesterday I had this whole-day testing session at my day work and it left me totally wasted. Stared at step 2 for hours without even understanding the problem. Only one gold star for me there. Today feels the same, with step 1. Might be enjoying this as a spectator sport from now. 😃

❤️ 1
🍿 1
2020-12-17T15:54:54.364200Z

hehe yeah I also feel like that after you lose one day is hard to recover

2020-12-17T15:55:30.365400Z

and I don't really have 1 hour or so every day to dedicate to this sadly (or I only do in the evening but I'm too tired to think)

2020-12-17T15:56:19.366100Z

maybe it should have a couple of breaks to allow people to get back on top of it

pez 2020-12-17T16:15:42.374100Z

I must say that picking aoc up was a great choice of mine. I've learnt so much from tackling the problems, from looking at your solutions, reading your comments, getting your feedback on my solutions, discussing the problems with you, discussing the solutions with you, I could go on. Spent tons of time on it. Do not regret one second. (Except yesterday, when I should have been wiser than to just sit there, staring at my failed reducing, instead of catching some needed sleep.)

➕ 5
2020-12-17T16:21:20.374400Z

yeah absolutely it's great for learning more about Clojure (or any other language tbf)

Aleks 2020-12-17T17:43:55.378Z

[2020 Day 17] The hardest part. https://i.redd.it/k6tlppouvq561.jpg

🤯 1
🤣 5
Aleks 2020-12-17T17:44:43.378400Z

Indeed!

2020-12-17T17:52:55.379200Z

That’s me !!!

Average-user 2020-12-17T21:30:14.382Z

I never really follow sample inputs, just use them to check if my algo gives the correct output for them

Average-user 2020-12-17T17:45:31.378900Z

jajaja

nate 2020-12-17T19:24:34.381300Z

Took a look at past results. Interesting to see what day into the month there was an order of magnitude fewer gold stars than the first day: 2020: day 17 2019: day 14 2018: day 18 2017: didn't happen, but got close on day 25 2016: didn't happen, but got close on day 25 2015: day 19

📉 1
misha 2020-12-19T10:27:31.430900Z

to get *second gold star

nbardiuk 2020-12-19T11:32:25.433300Z

yes, the colors are not consistent, one receives two gold stars per day. But there are silver and gold stars on stats page https://adventofcode.com/2020/stats

nbardiuk 2020-12-17T19:31:02.381400Z

To get the gold star for day 25 you don't have new puzzle, it is a bonus star for solving all puzzles of the year

2020-12-17T07:46:10.343900Z

> "Why to type [-1 0 1] when we can just simply (range -1 2) " > -- brainless myself

Aleks 2020-12-17T08:01:54.344600Z

Yes, but I prefer literals whenever it’s possible. Usually they are easier to read and catch.

2020-12-17T08:02:54.344800Z

you misread ... that's sarcasm

Aleks 2020-12-17T08:03:09.345Z

Ohh :)))

nbardiuk 2020-12-17T08:26:20.345400Z

😁 less characters and less off by one errors

erwinrooijakkers 2020-12-17T09:25:39.347600Z

hahaha

erwinrooijakkers 2020-12-17T09:25:42.347800Z

i did the same

euccastro 2020-12-17T10:42:11.349400Z

(juxt dec identity inc)? 😛

👍 4
misha 2020-12-17T11:25:46.349900Z

Yes

2020-12-17T12:08:49.350400Z

(take 3 (map dec (range)))

misha 2020-12-17T12:27:14.351200Z

No

Aleks 2020-12-17T12:30:06.351400Z

juxt: full version 😂 ((juxt dec identity inc) (count []))

🤣 3
euccastro 2020-12-17T14:41:34.357200Z

hehe, I was thinking of using that instead of adding deltas to get neighbours (I do use dec and inc in my solution), so I was actually half serious 😉

euccastro 2020-12-17T14:47:30.357500Z

i.e., (untested)

(defn cell-neighbors [cell]
  (remove #{cell}
          (apply combo/cartesian-product
                 (map (juxt dec identity inc) cell))))

This should work for any number of dimensions.

👍 1
❤️ 1
pez 2020-12-17T15:50:41.361600Z

Mensa dictionary entry 8: > Sarchasm : The gulf between the author of sarcastic wit and the person   >  who doesn’t get it.

😂 2