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
2017-12-13T02:54:53.000018Z

Nice.. I went from 10ms to 3ms using (and working around) a transient set in the hot loop

mikelis 2017-12-13T05:44:33.000040Z

I wonder if there’s a nice O(n) solution to part2

grzm 2017-12-13T06:05:49.000126Z

I suspect there is. I think I need to sleep before I finish part 2 😕

2017-12-13T07:03:56.000076Z

yeah there's a better way to do this I think.. it's taking a loooong time otherwise

2017-12-13T08:10:16.000236Z

ooh, came up with a relatively quick answer using not-any? and some

2017-12-13T08:10:29.000191Z

I bet there's a way you can just generate the answer using LCD's

fellshard 2017-12-13T08:11:27.000156Z

I was thinking the same thing, except I'm not sure that gets you the right answer still

fellshard 2017-12-13T08:12:29.000192Z

It could certainly help you find one point in time at which all the scanners would be at zero at the time your packet arrives, but I'm not sure that would be useful information, nor would it be guaranteed to be the minimum possible result

orestis 2017-12-13T08:42:04.000077Z

OK, this is the first time in AoC 2017 that my fans are spin-spin-spinning and I get no response from second part.

orestis 2017-12-13T08:42:48.000098Z

Time to do the simplest optimizations.

orestis 2017-12-13T08:55:45.000266Z

I’m sure there’s an algebraic way to solve this with no code whatsoever…

orestis 2017-12-13T08:57:33.000074Z

Using cycle was probably not a good idea 🙂

orestis 2017-12-13T09:43:07.000325Z

“Elapsed time: 536598.496201 msecs”

2017-12-13T10:17:24.000189Z

Execution time mean : 1.755792 sec

orestis 2017-12-13T10:18:10.000180Z

I dropped it to 8500msec and I need to get to real work 🙂

orestis 2017-12-13T10:18:42.000100Z

You can see all my progress in the same file: https://github.com/orestis/adventofcode/blob/master/clojure/aoc/src/aoc/2017_day13.clj

orestis 2017-12-13T10:19:00.000440Z

…but I do have to look at @minikomi’s code first 🙂

orestis 2017-12-13T10:21:58.000304Z

@minikomi I think we arrived at the same logic but your version is a bit more “unrolled” than mine.

2017-12-13T10:22:20.000055Z

yeah, not-any? was the key thing i did to speed it up

2017-12-13T10:22:43.000161Z

As soon as there's a zero-case. you can go onto the next one.

orestis 2017-12-13T10:30:28.000259Z

What’s your answer? Mine is close to 4 million…

orestis 2017-12-13T10:32:11.000209Z

I think that some should be equivalent in terms of performance with not-any? though.

orestis 2017-12-13T10:36:44.000295Z

Nice 🙂

2017-12-13T11:37:31.000007Z

3875838

mfikes 2017-12-13T14:05:00.000552Z

I wonder if there is an efficient solution to part 2 using some variation of the Chinese remainder theorem.

bhauman 2017-12-13T15:26:59.000292Z

6673ms for part 2

bhauman 2017-12-13T15:28:08.000423Z

@mfikes very nice solution for day13!

mfikes 2017-12-13T15:28:50.000518Z

Thanks! It luckily seems to perform well (around 800 ms for my data)

bhauman 2017-12-13T15:33:39.000577Z

@mfikes yeah you really don't have any intermediate data structures

bhauman 2017-12-13T15:33:59.000414Z

except for the last call to some

mfikes 2017-12-13T15:40:33.000647Z

Interesting. I didn't even think about that aspect. I just wrote it for readability / concision.

bhauman 2017-12-13T15:42:07.000023Z

our solutions are similar, you moved the logic a step deeper which eliminated the need to pass data around

bhauman 2017-12-13T15:50:44.000260Z

oh and I'm wasting cycles iterating through the count of the structure

grzm 2017-12-13T17:33:06.000247Z

\mutters read the instructions. read the instructions. read the instructions

mfikes 2017-12-13T17:46:16.000249Z

Don’t get caught ignoring the instructions.

grzm 2017-12-13T17:48:14.000824Z

Well played, sir. Well played.

grzm 2017-12-13T18:50:42.000533Z

Well, down from 21 seconds to 900ms for part 2. Now to see what solutions you guys came up with.

grzm 2017-12-13T18:56:04.000213Z

@mfikes is there a way you're preventing the github banner thing from displaying when you paste a link? That's really nice and tidy.

grzm 2017-12-13T18:59:09.000212Z

Wow, that's a nice, concise solution.

orestis 2017-12-13T19:17:59.000819Z

Where does some-fn come from?

orestis 2017-12-13T19:18:38.000479Z

Oh dear, it’s a core function…

orestis 2017-12-13T19:26:02.000505Z

I got thrown by the home-fn naming 🙂

borkdude 2017-12-13T20:17:19.000382Z

ok, finally done…

borkdude 2017-12-13T20:17:36.000091Z

2s for part 2

mfikes 2017-12-13T20:25:12.000124Z

@grzm After pasting a link, you’ll see a little delete button next to the GitHub banner.

grzm 2017-12-13T20:31:53.000474Z

@mfikes cheers.

grzm 2017-12-13T20:35:55.000661Z

@borkdude did your util/find-first function miss the commit?

mfikes 2017-12-13T20:36:13.000443Z

(I didn’t see it in there either FWIW.)

grzm 2017-12-13T20:38:42.000348Z

I like reading what others find useful to abstract away.

borkdude 2017-12-13T20:52:28.000218Z

@mfikes Sorry, fixed. See comment at the bottom why I used it.

borkdude 2017-12-13T21:01:49.000350Z

No idea why some was slower for me than that custom function

borkdude 2017-12-13T21:24:17.000387Z

(defn find-first
  [pred vals]
  (reduce
   (fn [_ v]
     (when (pred v)
       (reduced v)))
   nil
   vals))
(time (some       identity (repeat 10000000 nil))) ;; 250 ms
(time (find-first identity (repeat 10000000 nil))) ;; 40 ms

grzm 2017-12-13T21:28:36.000613Z

wow

borkdude 2017-12-13T21:37:36.000287Z

(time (some #(when (> ^long % 10000000) %) (range))) ;; 558 ms
(time (find-first #(> ^long % 10000000)    (range))) ;; 95 ms

bhauman 2017-12-13T21:40:59.000023Z

calls seq on every iteration

bhauman 2017-12-13T21:42:10.000294Z

calls (when (seq coll)) on every iteration to detect the end of the sequence

borkdude 2017-12-13T21:44:12.000005Z

I guess one more reason reduce is faster because many collections know how to reduce themselves

borkdude 2017-12-13T21:44:41.000305Z

but would there be a reason not to rewrite some using reduce so it’s faster?

chad 2017-12-13T21:52:44.000463Z

Hello! Im using Advent of Code as a way of learning Clojure and have just created a repo of what I have so far. Thought id post it incase anyone was interested 😃 https://github.com/sandemchad/clj-advent-of-code-2017

4👏
borkdude 2017-12-13T21:55:45.000477Z

@chad Feel free to add yours to this one too: https://github.com/adventofcode-clojurians/adventofcode-clojurians

borkdude 2017-12-13T21:59:07.000225Z

@mfikes holy crap, I just saw yours and it’s so concise

bhauman 2017-12-13T21:59:14.000435Z

@borkdude I changed my some implementation to reduce and nothing changed

mfikes 2017-12-13T21:59:25.000418Z

@chad Nice solutions, especially given you are learning

borkdude 2017-12-13T21:59:44.000428Z

@bhauman what if you execute above snippet? same timing?

mfikes 2017-12-13T22:00:21.000209Z

@borkdude Honestly, I think I just got lucky. I wrote what seemed straightforward, and by luck of the dice, it was short and fast. Doesn’t happen all the time. 🙂

bhauman 2017-12-13T22:01:35.000190Z

@borkdude I get similar results and it makes sense now that I look at your example

bhauman 2017-12-13T22:01:42.000221Z

more closely

chad 2017-12-13T22:02:27.000156Z

Awesome will do 🙂

chad 2017-12-13T22:04:38.000457Z

Thank you 🙂 I am really enjoying using Clojure solving these problems have been a really good way of learning.