Fork me on GitHub
#adventofcode
<
2020-12-17
>
Vincent Cantin07:12:10

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

alekszelark08:12:54

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

Vincent Cantin08:12:54

you misread ... that's sarcasm

nbardiuk08:12:20

😁 less characters and less off by one errors

euccastro10:12:11

(juxt dec identity inc)? 😛

👍 12
Daw-Ran Liou12:12:49

(take 3 (map dec (range)))

alekszelark12:12:06

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

9
euccastro14:12:34

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 😉

euccastro14:12:30

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.

👍 3
❤️ 3
pez15:12:41

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

😂 6
alekszelark12:12:11

did it as an exercise, cause I rarely write macros

👍 3
❤️ 3
Vincent Cantin12:12:29

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

Daw-Ran Liou12:12:13

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

Vincent Cantin12:12:28

it looks like firacode

erwinrooijakkers15:12:45

Can you copy the code, wondering what it does 🙂

erwinrooijakkers15:12:16

hard coded vector of [-1, 0 1]

erwinrooijakkers15:12:19

At compile time?

erwinrooijakkers15:12:22

Depending on the n?

alekszelark15:12:51

(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))

alekszelark15:12:54

it calculates adjacent locations for n-dimension space.

alekszelark15:12:34

You can use it like

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

(neighbours-3d [0 0 0])

Vincent Cantin15:12:36

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

alekszelark16:12:41

@U8MJBRSR5 can you show how to to do that?

Vincent Cantin16:12:16

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

alekszelark16:12:55

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])))))

👌 3
Vincent Cantin16:12:16

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

Vincent Cantin16:12:07

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

Vincent Cantin16:12:23

well .. it works with eval so why not.

Vincent Cantin17:12:19

@U067R559Q 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)

alekszelark17:12:15

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

alekszelark17:12:36

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

Vincent Cantin17:12:07

yeah, eval is ok during compile time

alekszelark17:12:33

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.

Vincent Cantin17:12:40

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)

pez15:12:54

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. 😃

🍿 3
❤️ 3
andrea.crotti15:12:54

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

andrea.crotti15:12:30

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)

andrea.crotti15:12:19

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

pez16:12:42

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.)

15
andrea.crotti16:12:20

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

alekszelark17:12:55

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

15
🤯 3
Vincent Cantin17:12:55

That’s me !!!

Average-user21:12:14

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

nate19:12:34

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

📉 3
nbardiuk19:12:02

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

misha10:12:31

to get *second gold star

nbardiuk11:12:25

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