My solution ^_^ https://github.com/zelark/AoC-2020/blob/master/src/zelark/aoc_2020/day_17.clj
so fast ! I can’t past part 1, I have a bug or a misunderstanding.
I don’t understand how the neighbors are counted, it seems inconsistent with the example .. maybe my english has a problem.
I believe your english much better than mine. I often have difficulties to understand what they want in the end.
I don’t understand why at z=-1 in their example, the first cube becomes active. I don’t see 3 active neighbors.
Time to work, will continue tonight.
Today I got it cleaner than yesterday, but the solution to the second one takes ~780ms https://github.com/euccastro/advent-of-code-2020/blob/master/src/advent/day17.clj
@zelark really neat!
I left the first solution untouched (just redefined what had to be generalized for the second one) and much of my time went to generalize the neighbors function, just because it seemed fun
and yeah, my cycle function would be neater had I realized that cells with 3 neighbors are always activated, whether they were already or not
Naive brute force (checking every neighbour for every point in every iteration) finished in 15 minutes or so for second part while I tried to optimize in the mean time https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2020/day17.clj Not sure how to optimize though, maybe stop counting when it’s more than 3? Any other suggestions? I’ll take a look later.
AH maybe use keep-indexed and not store \. as false to start with 🙂
Pure brute force for today: https://github.com/rjray/advent-2020-clojure/blob/master/src/advent_of_code/day17.clj
I've thought of a way to avoid looping over the entire coordinate system, but it'll have to wait until tomorrow after I've slept.
I solved it. My problem was just that the text did not mention that the slides of the examples were shifted down by 1.
https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_17.clj
@vincent.cantin Yes, it isn’t illustrated. Sometimes it can give you wrong assumptions. Actually I didn’t look at the examples, because the description was enough to me today.
Who wants more, here you go :)) https://adventofcode.com/2019/day/24
I will keep it for january
> "Why to type [-1 0 1]
when we can just simply (range -1 2)
"
> -- brainless myself
Yes, but I prefer literals whenever it’s possible. Usually they are easier to read and catch.
you misread ... that's sarcasm
Ohh :)))
😁 less characters and less off by one errors
hahaha
i did the same
(juxt dec identity inc)
? 😛
Yes
(take 3 (map dec (range)))
No
juxt: full version 😂
((juxt dec identity inc) (count []))
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 😉
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.Mensa dictionary entry 8: > Sarchasm : The gulf between the author of sarcastic wit and the person > who doesn’t get it.
@vincent.cantin love your solution, learned a lot from it, ty!
Seriously ?? Which part?
TIL from @zelark
did it as an exercise, cause I rarely write macros
I did not realize that I could use the multi-arity of = .. thanks !
Hey I like your typeface! The ligature looks so amazing. What’s the typeface you’re using?
it looks like firacode
Yeah, it’s FiraCode by @tonsky https://github.com/tonsky/FiraCode
Thanks!
Can you copy the code, wondering what it does 🙂
hard coded vector of [-1, 0 1]
At compile time?
Depending on the n?
(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))
it calculates adjacent locations for n-dimension space.
You can use it like
(def neighbours-3d (partial neighbours (adjacent 3)))
(neighbours-3d [0 0 0])
Yeah, the for
could be replaced by the list of literal of relative neighbor positions at compile time.
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. 😃
hehe yeah I also feel like that after you lose one day is hard to recover
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)
maybe it should have a couple of breaks to allow people to get back on top of it
@vincent.cantin can you show how to to do that?
I am still at work, I will try to do that after.
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.)
yeah absolutely it's great for learning more about Clojure (or any other language tbf)
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])))))
you don’t need the eval, just evaluate the sequence (and put it in a vector) outside of the backtick.
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
well .. it works with eval
so why not.
@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)
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.
btw, I found
(defn ~name ~@pre-args ~args ~(apply (eval (list
fn args expr)) args))` in clojure.core.
yeah, eval is ok during compile time
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.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)
[2020 Day 17] The hardest part. https://i.redd.it/k6tlppouvq561.jpg
Indeed!
jajaja
That’s me !!!
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
to get *second gold star
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
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
I never really follow sample inputs, just use them to check if my algo gives the correct output for them