Fork me on GitHub
#adventofcode
<
2022-12-15
>
wevrem06:12:54

Day 15 - Solutions

wevrem07:12:06

It took something like 10 minutes for my Part 2 to run. Yikes!

Apple07:12:48

If a position is occupied by B then that position doesn't count for part1, correct? How about the position of S?

wevrem07:12:16

I think just the B’s have to be removed.

wevrem07:12:32

I used pmap and got my part 2 runtime down to about 2.5 minutes. Still seems slow. https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2022/day_15_beacons.clj

norman07:12:57

https://gitlab.com/maximoburrito/advent2022/-/blob/main/src/day15/main.clj Not proud in any way of this code. Sad to say I spend OVER 30 minutes debugging part1 only to realize I was checking y=10 from the sample input and not the y for the real input... UGGH... I had to write a couple version of part2 before I got it right. I didn't reuse much code between part1 and part2. "Elapsed time: 31199.856226 msecs"

nbardiuk11:12:52

optimizing this problem is hard, I've reordered points and suddenly it solves in 4 seconds - just because my answer is close to edge and with the new order it needs to brute force through less data

Felipe13:12:11

part 2 takes some 30 seconds but hey, it works! my previous record was completing day 14 so I'm happy that's as far as I ever got 🎉 https://github.com/FelipeCortez/advent-of-code/blob/master/2022/15.clj

🎉 1
Alexis Schad13:12:57

(defn merge-intervals [intervals]
  (get
   (reduce (fn [[nb-open last retval] [x dir]]
             (if (= -1 dir)
               [(inc nb-open) (if (zero? nb-open) x last) retval]
               [(dec nb-open) last (if (= 1 nb-open) (conj retval [last x]) retval)]))
           [0 nil []]
           (->>
            (concat (map #(list (first %) -1) intervals)
                    (map #(list (second %) 1) intervals))
            (sort-by second)
            (sort-by first))) 2))

(defn part1 [rowy inputs]
  (let [intervals (keep (fn [[x1 y1 x2 y2]]
                          (let [dist (+ (abs (- x2 x1)) (abs (- y2 y1)))
                                dist-at-row (- dist (abs (- rowy y1)))]
                            (when (>= dist-at-row 0)
                              [(- x1 dist-at-row) (+ x1 dist-at-row)])))
                        inputs)]
    (merge-intervals intervals)))

(defn part2 [inputs]
  (doall
   (for [y (range 4000000)]
     (let [intervals (part1 y inputs)]
       (when (> (count intervals) 1)
         (println y intervals)))))
  nil)

(->> (slurp "day15.txt")
     (str/split-lines)
     (map (fn [line]
            (map parse-long (str/split line #" "))))
     (part1 2000000))

(->> (slurp "day15.txt")
     (str/split-lines)
     (map (fn [line]
            (map parse-long (str/split line #" "))))
     (part2))

misha14:12:39

finding slopes, their intersections, and the only one outside all sensors - 10ms

wevrem22:12:41

I tried out my idea that I was pondering last night, and Part 2 runs in 41 msecs. Yahoo!

wevrem22:12:23

Reading above, it looks like my approach must be similar to @U051HUZLD ’s

rjray22:12:41

I'm just flailing on this day, for some reason. My first submission for part 1 was too low. Every change/adjustment I've made to the code since then keeps returning the exact same answer.

genmeblog22:12:50

It was my case too... I had a bug in parsing, there are negative numbers.

genmeblog22:12:28

What happens on example data?

rjray22:12:45

Example data runs fine. And I am accounting for negative numbers, yeah.

rjray22:12:25

What is weirding me, is that every change yields the same answer. If there were something wrong with the algorithm I would expect SOME variation...

genmeblog22:12:27

Today's case is really hard to debug imho. I was reading squares manually to be sure I'm right (or wrong).

genmeblog22:12:16

You can take a look at my input and compare results.

genmeblog22:12:28

yields 4737443

rjray22:12:59

Which value for y?

tschady23:12:43

@UEF091BP0 not sure what you’re using, but sometimes when I get crazy results like that, it’s because I have some old function compile in my cache. restarting emacs cider takes care of it.

👍 1
bhauman23:12:39

@UEF091BP0 how are you handling becons in the row?

rjray23:12:03

Sadly, when I run for the actual answer I use a lein run process to make sure it's a clean VM.

rjray23:12:19

And I got the right answer for genme's input. Yay.

rjray23:12:38

I don't count beacons in the row... should I be? The example specifically doesn't count the one beacon in its row.

bhauman23:12:03

yeah that’s the way, just checking

rjray23:12:23

Hey, even I'm not sure at this point 😛.

bhauman23:12:01

hmmm and your are incrementing the difference between the start and end locations?

rjray23:12:33

Not sure what you mean?

genmeblog23:12:57

I had exactly the same, other imput was correct, mine not. Maybe verify parsing again?

rjray23:12:24

Hmmm... can't hurt. Let me check.

bhauman23:12:39

So the abs difference between -2 and 24 is 26. But its inclusive so its actually 27 entries minus the beacon.

bhauman23:12:24

Also, I think I’ve mentioned before about how edn/read-string is a great way to parse …. :face_with_hand_over_mouth:

bhauman23:12:06

yeah that was obnoxious

rjray23:12:13

Parsing checks out. What I am doing is running along the given y from (min-x, max-x). For each (x,y) pair, I look to see if it isn't a beacon, and if it is in range of at least one sensor. This gave me the correct answer for the other input data, just not for mine.

bhauman23:12:55

then I’d suspect the bounds maybe min-x and max-x are wrong

rjray23:12:46

Possible. They are calculated by finding the min-X from the sensors, then subtracting it's beacon distance from it. Do the same with max-X and +.

bhauman23:12:38

I can tell you a number that your max shouldn’t be below if you want. It’s something you learn in from the second part.

bhauman23:12:44

but that sounds like a great way to get the min and max

bhauman23:12:31

@UEF091BP0 hmmm actually your bounds algorithm sounds wrong

Apple23:12:41

anyone got more than one beacon on the same row?

bhauman23:12:19

@UEF091BP0 because the (min-x sensor + beacon dist) might not be the the absolute min-x

rjray23:12:56

Oh $^!%. I think it might actually be freakin' typo. Just found something sus when verifying my bounds code.

bhauman23:12:30

@UEF091BP0 did you see my messages above?

rjray23:12:19

Yes, I did. It's (min-x sensor minus beacon dist), then (max-x sensor plus beacon dist).

bhauman23:12:35

yeah that’s the problem, I had a typo

bhauman23:12:21

@UEF091BP0 (min-x sensor minus beacon dist) isn’t nessearily the min-x

bhauman23:12:07

@UEF091BP0 because the middle-x sensor may have a huge beacon dist

rjray23:12:49

Argh. And I must have just been lucky on genme's input, then.

rjray23:12:53

Whee. Part 1 done. How hard can part 2 be? Right?

🎉 1
bhauman00:12:10

good luck!

rjray00:12:49

Yeah, that was sarcasm 🙂. Even before reading part 2, I knew the gist of it. Not even sure where to start...

wevrem00:12:05

Finished writing a lot of notes to explain my approach. I’m quite pleased with it. https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2022/day_15_beacons.clj I need to go back and adapt part 1 to use it, I think I have a way to do it.

robertfw00:12:17

a pretty naive approach, checking each row for spans covered and merging those. pt2 runs in ~36 seconds edit: a bit of hacking away with some threads to use all my cores and I can get pt2 down to about 6.5s

👍 1
Casey13:12:48

Had no time yesterday.. playing catch up now. I came upon the same insight that was linked earlier on reddit.. checking one space beyond the perimeter of each range diamond. It's dog slow.. even using pmap . I also used the for some geometry functions. https://github.com/Ramblurr/advent-of-code/blob/main/src/aoc/2022/day15.clj My part 2 solution has a bug somewhere, because it returns multiple points for the sample.. but I lucked out I guess with my input because after 11 minutes running it spat out a single (correct) answer

Casey13:12:17

I'm not really sure why it is so slow.. if anyone is interested, I'd be curious to get some tips on why it's slow.

wevrem16:12:30

@U70QFSCG2 you don’t need to check every point outside a diamond (I think that is what you are doing) you can limit your search to intersections. In other words, expand the lines that make up each diamond by one point outward, then search the intersections of all those lines.

genmeblog19:12:10

I need some more correct examples for Day 15 part 1 can anyone share some part of his input with a result? My code works for given example but for my input not. All of ideas for debugging failed. Any hints?

genmeblog19:12:09

Just to be precise I need a couple of sensors with the result for any y

bhauman19:12:05

All of my input is in github

genmeblog19:12:05

ok, I'll try this, thanks

bhauman19:12:35

and my results are in my code

genmeblog19:12:08

Thanks! Funny (or not), the result from my code is the same... :/

bhauman19:12:11

been there for sure

😁 1
genmeblog19:12:25

geez... the bug is in parsing...

genmeblog19:12:51

I missed the negative - sign... which caused the error with my data.

genmeblog19:12:00

But not with yours.

genmeblog19:12:14

Anyway, thanks for assisting me 🙂

bhauman22:12:52

It was nothing! Glad you got it sorted. 🙂