adventofcode

Happy Advent 2020! Please put answers in the pinned threads or create one if it does not exist yet. | https://github.com/adventofcode-clojurians/adventofcode-clojurians | Join the private leaderboard with code 217019-4a55b8eb
2018-12-14T00:34:51.542Z

I'm not sure if I had more than 2 carts headed to the same point on the same tick, but I did have a bug when a moving cart collides with a cart that hasn't yet moved on the current tick. (The not-yet-moved cart was not being deleted correctly.)

2018-12-14T00:36:52.544100Z

You might try using group-by and (map count carts-generations) to verify that the cart collection cleanly removes exactly 2 carts instead of 1 or 3 at a time.

misha 2018-12-14T01:16:25.544400Z

> moving cart collides with a cart that hasn't yet moved just fixed that one :opieop:

misha 2018-12-14T01:20:02.546100Z

I just did loop/recur, could not come up with anything readable but not excessively wasteful with seq api

Average-user 2018-12-14T02:49:02.547200Z

I think Is pretty readable, and not that long: https://github.com/Average-user/adventofcode-clj-2018/blob/master/src/adventofcode_clj_2018/day13.clj

👍 2
taylor 2018-12-14T04:35:43.548800Z

day 13 animated https://www.youtube.com/watch?v=hnDNNvy8gww

2
Average-user 2018-12-14T04:48:42.549100Z

Nice

2018-12-14T06:09:11.549600Z

My part2 examples for day 14 make no sense

quoll 2018-12-14T06:09:45.550400Z

mine make sense, and I can pass them, but I can’t make it work on my puzzle input

2018-12-14T06:10:03.550800Z

Does someone have part1 solved and can send me just the descriptions please??

quoll 2018-12-14T06:10:32.551100Z

I’ve done part 1

quoll 2018-12-14T06:10:51.551400Z

descriptions of what?

2018-12-14T06:11:53.552200Z

part 1 and 2. The thing is that I probably need your part 1 input to understand the part 2 input because I guess we have different examples

quoll 2018-12-14T06:12:08.552400Z

DM

2018-12-14T06:12:40.552600Z

Thanks a lot

taylor 2018-12-14T06:16:08.553900Z

This one didn’t feel too hard, except it took me a while to understand part 2 objective

2018-12-14T06:16:09.554Z

I suspect we have an error.

2018-12-14T06:16:42.554700Z

Could you explain part2? My examples just give me the 5 five digits of each of the 10 digits from part two. That doesn’t seem right

taylor 2018-12-14T06:17:37.555900Z

You have to keep generating recipes until the sequence of individual digits from your input appear as scores

2018-12-14T06:18:35.556100Z

🙈 I still don’t get it

taylor 2018-12-14T06:18:57.556800Z

It’s not very well described either

taylor 2018-12-14T06:20:24.558100Z

If your input is 1982, then you have to keep generating recipes until your sequence of recipes contains a subsequence of [1 9 8 2]

2018-12-14T06:21:19.558300Z

one of my examples is “92510 first appears after 18 recipes.”

2018-12-14T06:21:25.558500Z

there’s no 18 there?!

taylor 2018-12-14T06:21:40.558900Z

Yeah the examples don’t make sense to me

taylor 2018-12-14T06:21:54.559400Z

I didn’t even bother running them

2018-12-14T06:22:37.559600Z

why five digits?

taylor 2018-12-14T06:22:55.559900Z

:man-shrugging:

2018-12-14T06:23:15.560300Z

@meikemertsch what it means is that the sequence ending “92510” has 18 prior recipe numbers

taylor 2018-12-14T06:23:22.560600Z

My input was six digits

2018-12-14T06:24:56.561Z

what would it look like for an input of 2?

quoll 2018-12-14T06:25:01.561300Z

is there anything odd about part 2?

2018-12-14T06:25:34.561900Z

The first sequence ending in 2 would be 37101012

2018-12-14T06:25:47.562300Z

so it would appear after 7 recipes

quoll 2018-12-14T06:26:14.562900Z

I ran my attempt over the 4 digits sequences in the examples, and it works fine. I run it over my 6 digit puzzle input, and it’s still going.

2018-12-14T06:26:33.563300Z

Your number will be very high

2018-12-14T06:26:49.563500Z

oh!!! Backwards!!!

2018-12-14T06:26:57.563700Z

I think I got it now

2018-12-14T06:27:16.563900Z

I’m still running part2, and I’m debating making more optimization or just waiting

quoll 2018-12-14T06:27:26.564100Z

yeah… but it’s a very tight loop, and it’s been going an hour

quoll 2018-12-14T06:27:50.564300Z

and will my sequence exhaust the heap?

2018-12-14T06:28:05.564500Z

Thanks for the help!!

quoll 2018-12-14T06:28:18.564700Z

perhaps I rewrite it in C 🙂

2018-12-14T06:28:40.565100Z

Thanks for the help. I understand the problem now

baritonehands 2018-12-14T06:30:19.565200Z

subvec really sped it up for me

2018-12-14T06:30:24.565400Z

I’m not sure how far. I’m tempted to look at other peoples number for guidance

baritonehands 2018-12-14T06:30:36.565700Z

21s

taylor 2018-12-14T06:30:58.566400Z

Part 2 finishes in a few seconds for me using vectors

2018-12-14T06:31:48.566600Z

hmm - I must be making a serious mistake in something. I’m only getting about 10k iterations/sec

2018-12-14T06:32:52.566800Z

(Thread/sleep 1000)

2018-12-14T06:33:09.567Z

that’s my debugging sleep so I can inspect the output

2018-12-14T06:33:09.567200Z

ha

2018-12-14T06:43:10.567500Z

🙈

quoll 2018-12-14T06:46:56.567700Z

@taylor, are you still here?

fellshard 2018-12-14T06:55:49.567900Z

Keep in mind you can have up to two digits appended to the end; you can't just check the very end of the vector for the pattern!

quoll 2018-12-14T07:03:38.568100Z

yes, I’d remembered that, but my 1am coding made a mistake with it. So while I was checking for more than just the very end, I wasn’t doing it right

quoll 2018-12-14T07:03:48.568300Z

and indeed, that was the problem

quoll 2018-12-14T07:04:00.568500Z

since my input ended in a 1

fellshard 2018-12-14T07:41:19.568700Z

Yeah, I did a dumb with that as well, a slipped paren

helios 2018-12-14T09:04:23.569700Z

hm, also for me part2 is taking long. I'm using subvec and I'm also not repeating the lookup in the middle of the vector, but only at the end

helios 2018-12-14T09:04:32.570Z

I wonder what more I could do

plexus 2018-12-14T09:05:34.570300Z

@helios you want to share what you have? I can take a quick look

helios 2018-12-14T09:06:01.570400Z

sure

helios 2018-12-14T09:06:17.570600Z

(ns adventofcode.2018.day14)

(def day-input 846601)
(def day-input-vec [8 4 6 6 0 1])

(set! *unchecked-math* true)

(def initial-state {:workers #{0 1}
                    :table   [3 7]})


(defn iteration [{:keys [workers table]}]
  (let [scores (map (partial nth table)
                    workers)
        recipe-sum (reduce + scores)
        new-recipes (if (< recipe-sum 10) [recipe-sum]
                                          [(quot recipe-sum 10) (rem recipe-sum 10)])
        new-table (reduce conj table new-recipes)]

    {:workers (mapv (fn [index] (mod (+ index (inc (nth table index)))
                                     (count new-table)))
                    workers)
     :table   new-table}))


(defn compute-score-after [n]
  (subvec (->> (iterate iteration initial-state)
               (drop-while #(< (count (:table %))
                               (+ 10 n)))
               first
               :table)
          n (+ n 10))
  )


(defn contains-sequence-at-end? [table sequence]
  (let [c1 (count table)
        c2 (count sequence)]
    (and (>= c1 c2)
         (= sequence (subvec table (- c1 c2)))))
  )


(defn solution2 [input]
  (->> (nth (iterate iteration initial-state) (count input))
       (iterate iteration)
       (drop-while (comp not #(contains-sequence-at-end? (:table %) input)))
       first
       :table
       (drop-last (count input))
       (count)))

helios 2018-12-14T09:06:58.570800Z

@plexus a gist is better maybe: https://gist.github.com/Heliosmaster/4971270311f00f3f8af1ccb7c0d29adf

plexus 2018-12-14T09:13:30.571200Z

Looking at it from Visualvm it seems your bottleneck is in iteration. table is always a vector, right? In that case you should avoid nth, as it will treat the vector as a seq and walk it element by element. Use get instead.

fellshard 2018-12-14T09:17:50.571400Z

You're testing for the pattern at the end, but you're adding not just one, but possibly two digits at the end.

helios 2018-12-14T09:19:16.571600Z

@fellshard right, i should check twice to be sure

helios 2018-12-14T09:19:25.571800Z

because only the first digit might be sufficient

helios 2018-12-14T09:19:25.572Z

thanks

helios 2018-12-14T09:29:12.572200Z

and also when that happens, I was off-by-1 with the final solution 😄

borkdude 2018-12-14T09:41:12.572500Z

@plexus Is that right, I thought nth on vector is comparable to an array lookup: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentVector.java#L161

☝️ 1
plexus 2018-12-14T09:47:37.572800Z

I guess you're right!

plexus 2018-12-14T09:54:39.573Z

it's still spending a lot of its time in those first few lines of iteration, just doing the map+`reduce` to compute the score...

helios 2018-12-14T12:00:18.573400Z

the reduce on the scores should be very fast (it's just two values)

helios 2018-12-14T12:00:36.573600Z

and also map should just operate on two scores

benoit 2018-12-14T13:11:46.574100Z

Mine takes 34s w/ subvec 😞 https://github.com/benfle/advent-of-code-2018/blob/master/day14.clj

Average-user 2018-12-14T13:12:38.574400Z

Is (set! *unchecked-math* true) really necessary?

taylor 2018-12-14T13:49:48.575700Z

I’m getting ~10s for part 2 w/vectors, subvec, loop, recur

misha 2018-12-14T14:03:17.576100Z

"Elapsed time: 36315.651048 msecs" not optimized for performance p2 (somewhat readable though)

misha 2018-12-14T14:07:34.576600Z

not relevant, but this could be just def https://github.com/benfle/advent-of-code-2018/blob/master/day14.clj#L14-L17

benoit 2018-12-14T14:12:01.576900Z

absolutely 🙂

1
quoll 2018-12-14T16:52:38.579200Z

I got 4.3s in the end (2am) 😳 I had an optimization to only look when one of the last 2 digits matched the final digit of my input, and I messed it up, which is what kept me up late. But when it worked, it was fast

misha 2018-12-14T17:18:56.579800Z

@quoll nice

quoll 2018-12-14T17:26:35.579900Z

I wanted to avoid unnecessary subvecs, so I finished my loop with something like this pseudocode:

(if (= last-input-digit last-digit-added)
  (test-the-trailing-subvec recipes input)
  (if (> last-value-added 9) ;; we know that the final input digit is 1
    (test-the-second-last-trailing-subvec recipes input)
    (recur recipes first-elf second-elf)))

2018-12-14T17:45:19.580100Z

I stole your adjust-idx function and it gave me a 10x speed up. Seems like my run cost was dominated by the rem function call to wrap the index back into the correct range. Successive subtraction is far cheaper. Now I get 2-3 seconds for part 2, down from 25-30 seconds.

misha 2018-12-14T17:53:41.580300Z

nice time! adjust-idx probably could be a bit faster with loop in it instead of seqs, but I didn't bother to test it out

2018-12-14T17:59:00.580700Z

https://www.twitch.tv/timpote

2018-12-14T18:11:22.580900Z

I retract everything! I introduced a typo when using adjust-idx that resulted in my algorithm finding the wrong answer, which appeared much earlier in the sequence. So the execution overhead didn't change significantly, I was just running through 1/10th the iterations. I'm back above 20 seconds! :man-facepalming:

misha 2018-12-14T18:26:23.581100Z

:opieop:

fellshard 2018-12-14T19:03:31.581400Z

That's probably a lot cheaper...

fellshard 2018-12-14T19:13:23.584200Z

One idea I had, that may be vaporware - you can 'replay' strings you've seen before, up to the point where one of the elves wrapped around to the beginning. Since those substrings have already been seen, the pattern could only show up at the boundary between that memoized string and the current recipe string. This would probably accelerate as you got further down the line... Then again, might be the cost of holding all that in memory / calculating all these strings would be inordinately costly. Managing them would certainly be a pain.

pesterhazy 2018-12-14T19:22:19.585Z

Proudly unoptimized

taylor 2018-12-14T19:33:03.585500Z

very concise 😚👌

2018-12-14T19:41:43.585600Z

Dang I missed it. But I also needed a day off programming. Did today’s part 2 literally at work so I would have the evening free while I was waiting for a build (several times) containing some fixes from one of my devs. This was my last working day today so there was no pressure to start anything new

2018-12-14T19:54:48.585800Z

Nice! Yeah I’ve had to take a few days from AoC myself. Glad to have a little lead time before the stream 🙂

misha 2018-12-14T20:46:15.586200Z

but what does all of it mean? :kappa:

2018-12-14T21:18:22.586900Z

Is it wrong that my first response to being told that my answer is too big is to try (dec answer) ?

2018-12-14T21:18:33.587200Z

And then not fix the bug...

markw 2018-12-14T21:20:42.587800Z

@magic_bloat wrong: probably effective: yes

😁 1
markw 2018-12-14T21:49:58.588500Z

note to self... if you use (take-last) on day 14, you're gonna have a bad time.

1
☝️ 1