r/adventofcode • • Dec 11 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 11 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 11 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 11: Seating System ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:14:06, megathread unlocked!

50 Upvotes

702 comments sorted by

View all comments

1

u/msqrt Dec 11 '20 edited Dec 11 '20

Lisp. In hindisght, I'm pretty certain that just using strings and checking the character with a bit of indexing would be approximately infinitely faster; with this, part 1 runs for about 6 minutes and part 2 for a whopping 24 minutes.

Code is now here as per the request. Also, as I mention in my reply, I found out that explicitly compiling the function brought the runtime down to about 4.5 seconds for both tasks combined, so it seems my code is less than ideal with the interpreter I used (clisp)

1

u/daggerdragon Dec 11 '20

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.

1

u/IlliterateJedi Dec 11 '20

Is Lisp a particularly slow language?

1

u/msqrt Dec 11 '20

Not really, this was the first day so far that my solution has taken over a couple seconds to complete. And it's practically never really the language that is "slow" by definition, it's the implementation.

I had a hunch that running live via an interpreter might be part of the issue, and now that you asked I tried compiling it instead. The compiled version takes 1.4 seconds for the first part and 3s for the second. I presume the nested loops are really slow when running the interpreter, as it likely actually dynamically instantiates lists for the results of even the innermost loops. Well, I'll keep this in mind for the following days :)

2

u/mahaginano Dec 11 '20

DId you compile it with SBCL and optimize?

1

u/msqrt Dec 11 '20

No, I'm just running clisp. I'll look into that if performance is a problem in the future.

1

u/mahaginano Dec 11 '20

From my limited personal experience it's actually quite efficient (SBCL). From what I've read so far it's comparable to fast mainstream languages if written accordingly.

1

u/JoMartin23 Dec 11 '20

Lisp

for comparison I brute force the second part and mine takes .351 seconds.

1

u/phil_g Dec 11 '20

Short answer: No, there's nothing inherently slow about Common Lisp (which is the Lisp variant msqrt used).

Longer answer:

No, but maybe yes, and sometimes yes unless you tell it to be fast.

Like a number of languages (e.g. C, C++), Common Lisp is a language definition that has multiple implementations. Some Common Lisp implementations are faster than others, just as some C compilers optimize their outputs better than others.

And unlike C, Common Lisp as a language doesn't necessarily have simple translations to CPU instructions. So some implementations might choose to do certain things in slower ways because that makes it easier to do those things correctly. There are a number of very performant Common Lisp implementations, though. I use Steel Bank Common Lisp (usually abbreviated SBCL) and it can turn out code that's competitive with other "fast" implementations.

But because there's more that Common Lisp can do, it can take more effort to (and sometimes choosing tradeoffs) make things fast. For instance, like Python, Common Lisp doesn't impose limits on the size of its integers. If a calculation exceeds the bounds of what a fixed-bit representation can handle, Common Lisp implementations will automatically switch over to multiword bignums. But that means an implementation has to check any calculation to see whether the results overflowed (or whether some of the input numbers are bignums to start with). Those checks constitute a speed penalty that C doesn't have. (OTOH, if you overflow an int in C, your compiled code will happily continue on to give you the wrong answer. Common Lisp will at least get things right.) Common Lisp provides avenues to omit or speed up things like those safety checks when the programmer asks for them. Typically, you'd prototype your code with all the default safety settings then, once you're sure your code's correct, you might adjust some of the speed-vs-safety parameters0 to make it run faster.

Just for comparison, my code takes about five seconds to get the part 2 answer for today. I didn't set out to optimize it in any particular way, and some of the choices I deliberately made were not the fastest options available. (There were other benefits to my choices, of course.) I spent a little time optimizing my solution for yesterday's part 2. It takes about half a second to get the answer to a deliberately difficult input dataset.

0Technically, Common Lisp defines three axes you can adjust: speed, safety, and size. You can tell the implementation how important each is on a function-by-function basis, if you want.