adventofcode

wevrem 2023-12-11T05:42:47.725939Z

Day 11 - Solutions

erdos 2023-12-11T05:43:18.878799Z

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
wevrem 2023-12-11T05:45:25.872939Z

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

πŸ‘€ 1
erdos 2023-12-11T05:57:26.378009Z

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)

wevrem 2023-12-11T06:05:34.554169Z

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
erdos 2023-12-11T06:12:29.900729Z

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?

wevrem 2023-12-11T06:13:49.498509Z

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.

wevrem 2023-12-11T06:18:16.042209Z

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

πŸ‘ 1
Aleks 2023-12-11T06:43:36.135669Z

not fancy, but it just does its job https://github.com/zelark/AoC/blob/master/src/zelark/aoc_2023/day_11.clj

Aleks 2023-12-11T07:25:19.860819Z

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

alpox 2023-12-13T17:40:03.440049Z

This was quite a bit more chill than d10

roklenarcic 2023-12-12T08:04:27.495799Z

I accumulated coordinate adjustments with reductions: https://gist.github.com/RokLenarcic/00a3a14c975d0ace582ff9a3695226b0

Felipe 2023-12-12T18:49:39.824319Z

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

πŸ‘ 1
😁 1
tschady 2023-12-11T09:31:51.574919Z

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
genmeblog 2023-12-11T10:14:03.011189Z

Same as others I suppose. https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2023/day11.clj

Aleks 2023-12-11T10:47:27.390869Z

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

2023-12-11T11:26:40.059219Z

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

Ivana 2023-12-11T11:51:59.303769Z

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

borkdude 2023-12-11T14:07:33.159339Z

I took @erdos his solution and added stuff to #squint 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=OzsgSGVscGVyIGZ1bmN0aW9uczoKOzsgKGZldGNoLWlucHV0IHllYXIgZGF5KSAtIGdldCBBT0MgaW5wdXQKOzsgKGFwcGVuZCBzdHIpIC0gYXBwZW5kIHN0ciB0byBET00KOzsgKHNweSB4KSAtIGxvZyB4IHRvIGNvbnNvbGUgYW5kIHJldHVybiB4Cgo7OyBSZW1lbWJlciB0byB1cGRhdGUgdGhlIHllYXIgYW5kIGRheSBpbiB0aGUgZmV0Y2gtaW5wdXQgY2FsbC4KKGRl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YnMgKC0gYSB4KSkKICAgICAgKGFicyAoLSBiIHkpKQogICAgICAoKiAoZGVjIHNjYWxlKSAoKyBhYSBiYikpKSkpCgooY29tbWVudAogIChtYW5oYXR0YW4yIChmaXJzdCBnYWxheGllcykgKHNlY29uZCBnYWxheGllcykgMikKICApCgooZGVmbiBzb2x2ZSBbbGFiZWwgc2NhbGVdCiAgKC0%2BPiAoZm9yIFthIGdhbGF4aWVzLGIgZ2FsYXhpZXNdIChtYW5oYXR0YW4yIGEgYiBzY2FsZSkpCiAgICAocmVkdWNlICspCiAgICAoKiAxLzIpIChsb25nKQogICAgKHRpbWUpKSkKCihjb21tZW50CiAgKHNvbHZlICJGaXJzdCIgMikKICAoc29sdmUgIlNlY29uZCIgMTAwMDAwMCkKICAp

πŸ’ͺ 1
Felipe 2023-12-11T16:03:14.173749Z

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

Felipe 2023-12-11T16:06:57.114459Z

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)

Ivana 2023-12-11T16:27:20.897299Z

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
Felipe 2023-12-11T16:28:43.694989Z

@andrey.yallowsack.com nice! makes sense

rjray 2023-12-11T19:06:18.480379Z

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.

Arnaud Geiser 2023-12-11T21:28:30.110479Z

It went quite well, thanks to clojure.math.combinatorics : https://github.com/arnaudgeiser/advent-of-code/blob/master/2023/clojure/src/advent_of_code/day11.clj

Ivana 2023-12-11T21:33:30.839099Z

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

πŸ‘ 1
Arnaud Geiser 2023-12-11T21:36:48.845869Z

Thank you Ivana for the hint!

Ivana 2023-12-11T21:38:07.183829Z

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

Arnaud Geiser 2023-12-11T21:39:44.204969Z

Works like a charm : https://github.com/arnaudgeiser/advent-of-code/blob/master/2023/clojure/src/advent_of_code/day11.clj#L25

πŸ‘ 1
bhauman 2023-12-11T22:40:14.264249Z

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

2023-12-12T03:28:22.111629Z

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
2023-12-12T04:03:09.195319Z

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.