Fork me on GitHub
#adventofcode
<
2023-12-11
>
wevrem05:12:47

Day 11 - Solutions

erdos05:12:18

https://github.com/erdos/advent-of-code/blob/master/2023/day11.clj used sorted-set to quickly find the empty rows/cols in the path between two galaxies.

1
wevrem05:12:25

I like the use of sorted set. I’ve never used that before. I just rolled my own. :-)

👀 1
erdos05:12:26

sorted-set only helps if you keep track of the gaps instead of the galaxies (this universe is dense so there are much more galaxies than gaps)

wevrem06:12:34

that makes sense. I also just kept track of the gaps, but not with a sorted set. Just with a sorted list and then (this is what I meant by “rolled my own”) I filtered based on the two subject galaxies. I assume that is basically what a sorted set does, though perhaps more efficiently?

👍 1
erdos06:12:29

yes, sorted-set is practically a tree that can be used for fast interval search. but since you used filter , sorting the seq was really not necessary?

wevrem06:12:49

true. I think it was left over from an initial plan of maybe using drop and take. Just stayed in the code there even though it was no longer needed.

wevrem06:12:16

Tomorrow I’m going to learn more about sorted set and play around with a version 2. Good night!

👍 1
alekszelark07:12:19

@U2E1D7WUB I like your solution. Don’t remember if I’ve ever used subseq before. Looks like a good place to employ it

tschady09:12:51

first pass, used the excellent tails I got from somebody here last year to get the combos: https://github.com/tschady/advent-of-code/blob/main/src/aoc/2023/d11.clj

👍 1
alekszelark10:12:27

Got rid of the extra passes of the input. All we need is galaxies coordinates, so we can build sets of it and then use it to find spaces https://github.com/zelark/AoC/blob/master/src/zelark/aoc_2023/day_11.clj

Piotr Kaznowski11:12:40

I chose "brutal" list operations to get coordinates instead of smart arithmetic... https://github.com/caseneuve/aoc2023/blob/master/day11/solution.clj

Ivana11:12:59

After some fixes & improvements

(let [scale 1000000 ;; 2
      gals (->> input (keep-indexed #(when (= \# %2) %1)))
      d (-> input (str/index-of \newline) inc)
      [grs gcs] (map (fn [f] (->> gals (map #(f % d)) set)) [quot rem])
      dn (fn [a b s] (->> (range (min a b) (max a b))
                          (reduce (fn [acc i] (+ acc (if (get s i) 1 scale))) 0)))]
  (->> (for [a gals
             b gals
             :when (< a b)]
         (let [[[r1 c1] [r2 c2]] (map #(vector (quot % d) (rem % d)) [a b])]
           (+ (dn r1 r2 grs) (dn c1 c2 gcs))))
       (apply +)))

borkdude14:12:33

I took @U2E1D7WUB his solution and added stuff to #C03U8L2NXNC until it worked (mostly sorted-set and subseq) https://squint-cljs.github.io/squint/?repl=true&amp;boilerplate=https%3A%2F%2Fgist.githubusercontent.com%2Fborkdude%2Fcf94b492d948f7f418aa81ba54f428ff%2Fraw%2Fa6e9992b079e20e21d753e8c75a7353c5908b225%2Faoc_ui.cljs&amp;src=OzsgSGVscGVyIGZ1bmN0aW9uczoKOzsgKGZldGNoLWlucHV0IHllYXIgZGF5KSAtIGdldCBBT0MgaW5wdXQKOzsgKGFwcGVuZCBzdHIpIC0gYXBwZW5kIHN0ciB0byBET00KOzsgKHNweSB4KSAtIGxvZyB4IHRvIGNvbnNvbGUgYW5kIHJldHVybiB4Cgo7OyBSZW1lbWJlciB0byB1cGRhdGUgdGhlIHllYXIgYW5kIGRheSBpbiB0aGUgZmV0Y2gtaW5wdXQgY2FsbC4KKGRlZiBpbnB1dCAoLT4%2BIChqcy1hd2FpdCAoZmV0Y2gtaW5wdXQgMjAyMyAxMSkpCiAgICAgICAgICAgICBzdHIvc3BsaXQtbGluZXMpKQooZGVmIGdyaWQgKG1hcHYgdmVjIGlucHV0KSkKCihkZWYgZW1wdHktcm93cwogIChpbnRvIChzb3J0ZWQtc2V0KQogICAgKGtlZXAtaW5kZXhlZCAoZm4gW2kgcm93XSAod2hlbiAoZXZlcnk%2FICN7XC59IHJvdykgaSkpKQogICAgZ3JpZCkpCgooZGVmbiB0cmFuc3Bvc2UgW210eF0KICAodmVjCiAgICAoZm9yIFtpIChyYW5nZSAoY291bnQgKGZpcnN0IG10eCkpKV0KICAgICAgKHZlYyAoZm9yIFtqIChyYW5nZSAoY291bnQgbXR4KSldCiAgICAgICAgICAgICAoLT4gbXR4IChudGggaikgKG50aCBpKSkpKSkpKQoKKGRlZiBlbXB0eS1jb2xzCiAgKGludG8gKHNvcnRlZC1zZXQpCiAgICAoa2VlcC1pbmRleGVkIChmbiBbaiBjb2xdICh3aGVuIChldmVyeT8gI3tcLn0gY29sKSBqKSkpCiAgICAodHJhbnNwb3NlIGdyaWQpKSkKCihkZWYgZ2FsYXhpZXMKICAoZm9yIFtbaSByb3ddIChtYXAtaW5kZXhlZCB2ZWN0b3IgZ3JpZCkKICAgICAgICBbaiBjZWxsXSAobWFwLWluZGV4ZWQgdmVjdG9yIHJvdykKICAgICAgICA6d2hlbiAoPSBcIyBjZWxsKV0KICAgIFtpIGpdKSkKCihjb21tZW50CiAgKGNvdW50IGdhbGF4aWVzKQogICkKCihkZWZuIG1hbmhhdHRhbjIgW1thIGJdIFt4IHldIHNjYWxlXQogIChsZXQgW2FhIChjb3VudCAoc3Vic2VxIGVtcHR5LXJvd3MgPiAobWluIGEgeCkgPCAobWF4IGEgeCkpKQogICAgICAgIGJiIChjb3VudCAoc3Vic2VxIGVtcHR5LWNvbHMgPiAobWluIGIgeSkgPCAobWF4IGIgeSkpKV0KICAgICgrIChhYnMgKC0gYSB4KSkKICAgICAgKGFicyAoLSBiIHkpKQogICAgICAoKiAoZGVjIHNjYWxlKSAoKyBhYSBiYikpKSkpCgooY29tbWVudAogIChtYW5oYXR0YW4yIChmaXJzdCBnYWxheGllcykgKHNlY29uZCBnYWxheGllcykgMikKICApCgooZGVmbiBzb2x2ZSBbbGFiZWwgc2NhbGVdCiAgKC0%2BPiAoZm9yIFthIGdhbGF4aWVzLGIgZ2FsYXhpZXNdIChtYW5oYXR0YW4yIGEgYiBzY2FsZSkpCiAgICAocmVkdWNlICspCiAgICAoKiAxLzIpIChsb25nKQogICAgKHRpbWUpKSkKCihjb21tZW50CiAgKHNvbHZlICJGaXJzdCIgMikKICAoc29sdmUgIlNlY29uZCIgMTAwMDAwMCkKICAp

💪 1
Felipe16:12:14

took me a while to realize that

(defn shortest-path-length [[j1 i1] [j2 i2]]
  (let [[[j1 i1] [j2 i2]] [[(min j1 j2) (min i1 i2)] [(max j1 j2) (max i1 i2)]]]
    (count
     (take-while #(not= % [j2 i2])
                 (iterate (fn [[p1 p2]]
                            (if (< (dist [(inc p1),     p2 ] [j2 i2])
                                   (dist [     p1, (inc p2)] [j2 i2]))
                              [(inc p1),      p2]
                              [     p1,  (inc p2)]))
                          [j1 i1])))))
is just
(defn shortest-path-length [[j1 i1] [j2 i2]]
  (+ (abs (- j1 j2)) (abs (- i1 i2))))
map expansion logic was also a bit tricky to get right. happy with my solution! https://github.com/FelipeCortez/advent-of-code/blob/master/2023/11.clj

Felipe16:12:57

I always have the ugliest code to deal with grids represented as nested vecs. this is very nice:

[[i row] (map-indexed vector grid)
 [j cell] (map-indexed vector row)

Ivana16:12:20

There were already a lot of tasks with 2D-array model input. And most of the participants parsed it from string to vector of vectors, and then feel pain vith indexing, iteration, filtering etc. I suggest you keep it as it is - as string 😀 It supports get by index, it implements associative? and return nils on indices outside of its range, it may be easy processed with keep-indexed , str/index-of , regex etc. Only thing you have to do for modelling 2D array with it - convert one linear index to 2 2D. But keep in mind, that even raw C-like 2D arrays are actually 1D arrays with indices converting by quot & rem , so you may do it by yourself and get away from naive "2D" arrays model of most langs (Java/C/etc). You may look into my this aoc tasks code solutions for examples & details

😨 1
Felipe16:12:43

@U05T4JC21J6 nice! makes sense

rjray19:12:18

My solution: https://github.com/rjray/advent-2023-clojure/blob/master/src/advent_of_code/day11.clj Not my best code, because I did part 1 by actually expanding the matrix representation of the input "image". The pure-math code for part 2 can easily be made to work with part 1. I'll do a revision of this one later.

Ivana21:12:30

(comb/permuted-combinations locations 2)
may be simply implemented as
(->> (for [a locations
             b locations
             :when (< a b)]
without extra libs

👍 1
Arnaud Geiser21:12:48

Thank you Ivana for the hint!

Ivana21:12:07

Actually in your case when location is vector < may not work but you may use (pos? (compare a b))

bhauman22:12:14

Day 11 - for part 2 I just translated the initial coords into the new expanded coord system. https://github.com/bhauman/adv2023/blob/main/src/adv2023/day11/sol.clj

stand03:12:22

I know from many years of running that if you want to get from point A to point B, it's the same number of blocks regardless of what path you take. 😃

😆 1
m_traven04:12:09

https://github.com/mtravers/aoc2023/blob/main/src/aoc2023/day11.clj Fortunate that I did part 1 in a way that made part 2 trivial.

Felipe18:12:39

@U0608BDQE relevant xkcd, maybe https://xkcd.com/85/

👍 1
😁 1
alpox17:12:03

This was quite a bit more chill than d10