r/adventofcode • u/coriolinus • Dec 09 '18
Day 9: how fast does your solution run?
My code calculates the answer to both parts in about 600 milliseconds. At first, I thought that I must have missed something; that's the slowest of any day's answers by a factor of five. Then I started browsing a little here and discovered people talking about runtimes measured in minutes, and I started feeling less bad.
Still, I am not the world's best coder, and I am certainly not the world's best optimizer. How low a runtime have you guys solved this problem in, and are you willing to share the techniques that got you there?
6
Upvotes
10
u/ivosu Dec 09 '18
Based of the fact, that the marble placement is the same every time, I would assume there is some BAT SH!T CRAZY formula, that could calculate score for each player. So it could be really done in O(m) where m is number of players. But I don't have that formula...
5
u/krakedhalo Dec 09 '18
Whoa.... I know I'm new at this and all, but my code for part 2 has been running for half an hour and is 1/10th of the way to 100x.
Now clearly there's something I'm missing, and I know my code is beginner level, but 600ms?!?
Y'all are some wizards.
4
u/irrelevantPseudonym Dec 09 '18 edited Dec 10 '18
Mine took 1.65s in python and 60ms in rust. My rust version is here. I'm very much still learning rust so not sure about all the
as u32/u64/usizestuff.Edit: code moved to gist
4
u/sparr Dec 09 '18
This one, more than any other, wants a low level language with highly optimized code.
4
u/toastedstapler Dec 09 '18 edited Dec 10 '18
implementing my own linked list in python took 17s for part 2, an inbuilt deque took just 1.65s
e: made linked list in java, took about 0.3s to run
4
u/tyr10563 Dec 09 '18
I've managed to lower my runtime from ~470 ms down to ~160 ms using the C++17 memory_resources.
Full code is here.
3
u/chameth Dec 10 '18
I went a bit overboard and eventually got mine down to about 50ms total for both parts, using Nim and doing some nasty things with memory allocation and array pointers.
The code's here, and I wrote up the various steps I went through to optimise it, as it was a fun journey.
3
u/p_tseng Dec 10 '18 edited Dec 11 '18
tl;dr to answer your question, about 1.5 seconds interpreted and 140 ms compiled, by using a singly-linked list which is backed by a single array (rather than individual node objects). The long story:
In terms of contributions from others that I'd like to see in this thread, I enjoyed https://www.reddit.com/r/adventofcode/comments/a4kyij/aoc_2018_day_9_solution_1_2_linked_list_approach/ebgscmc/ . The use of a fixed-size array + current pointer + size to simulate the double-ended queue is excellent.
Here is what I have to add:
(All timings are the time it takes to output the answers to part 1 and part 2; I do not take stats for only completing one of the two parts)
I tried a lot of implementations.
Where can we do better? I thought I'd explored most avenues for the double-ended queue (I actually reverse the direction and use Ruby's negative array indexing so that I don't have to do as many modulo operations) and the zipper (I mentioned the strategic choice of focus so we only move_right once). So let's look at the doubly-linked lists.
We want to do as little work in each loop iteration as possible.
The doubly-linked list is workable but having to update two pointers on insert and two on delete is a shame.
Why do we need the left pointer? Why, so that we can move left, of course. But we only move left when removing a marble. Can we rephrase that in terms of moving right?
Well, take a look at the example. The marble removed is the marble to the right of the marble that was the current marble 5 iterations ago. And turns out, this remains the case forevermore.
Thus, we need only a singly-linked list. Now we only have half as many pointers to update per loop iteration.
Since we only have one additional pointer associated with each marble, as before those pointers can be stored in one big array, pre-allocated so that we don't have to allocate on-demand.
The implementation in Ruby https://github.com/petertseng/adventofcode-rb-2018/blob/master/09_marble_mania.rb runs in 1.5 seconds, doing better than any other implementation I tried. Finally, let's try having it compiled.
I don't feel like rewriting the whole thing in my compiled language of choice so instead I just made the minimal edits to the Ruby code to get it to compile in Crystal at https://github.com/petertseng/adventofcode-rb-2018/blob/crystal/09_marble_mania.cr (remember to pass
--releasetocrystal buildsince we are wanting to be time the resulting binary), now it takes about 140 ms to run.So the singly-linked list can be an interesting approach, and I'd like to see what else we can explore in similar areas.
2
u/Pyrobolser Dec 09 '18
Same surprise for me, when I saw the Part 2 I already had bad memories from previous years of AoC. But this time everything went smoothly.
1.62s in C# for both parts, you can see the code here on GitHub.
2
u/HeyItsBATMANagain Dec 09 '18 edited Dec 09 '18
I first wrote this in TypeScript for NodeJS, which has a runtime of somewhere between 600 and 700ms, but since I felt this was too slow for me and I wanted to learn some new compiled language anyways I rewrote it in crystal, which compiled with --release and --no-debug (whatever this does), runs part 1 in ~1300µs and part 2 in ~135ms. Pretty happy with it, though it's still one of the longer runtimes
Reddit formatting is hard oof
2
u/permetz Dec 09 '18
Mine does the tests + part 1 in 27ms, tests + part 1 + part 2 in 1.7 seconds which is significantly less than 100x as long. Code written in OCaml in pure functional style using a zipper. Relatively slow machine, it's a 6 year old macbook pro.
1
u/[deleted] Dec 09 '18 edited Jun 11 '23
fuck u/spez
1
u/tslater2006 Dec 09 '18
Part 1 runs in 0.50 seconds, part 2 runs in 50 seconds.
I'm using a shitty interpreted language oriented towards business logic and not performance. (PeopleCode)
1
u/Frizkie Dec 09 '18
Just a hair over 2s with my Ruby implementation.
1
u/BOT-Brad Dec 09 '18
JavaScript running in Node 8.11
Part 1: ~15ms (Averaged over 50 runs)
Part 2: ~ 650ms (Averaged over 50 runs)
1
u/ollien Dec 09 '18
950ms for both parts in Go. I might be able to speed it up if I store the element seven elements over, but I don't have the time to play with that atm.
1
u/CryZe92 Dec 09 '18 edited Dec 09 '18
We did this together in the Rust Community Discord, this takes about 250µs for part 1:
The memory overhead isn't worth it for part 2 though, where this implementation is around 60ms:
1
u/Pepa489 Dec 09 '18
My original naive solution in typescript took 3+ hours. With custom circular doubly linked list it's something around 3 secs
1
u/surrix Dec 10 '18
My solution to part 1 ran in about 0.05 sec. My solution to part 2 has been running for almost 40 minutes now. I must have a truly horrendous O(*) implementation.
1
u/justinhj Dec 10 '18
Part 2 involves building up to a 7.1 million list doing deletes and inserts, so it’s going to be slow unless you can do those operations efficiently. As far as I know the most efficient data structure for this would be the double linked list.
I am working in Ruby which isn’t that fast anyway but I was surprised that a simple version using array insert and delete_at took 2 hours to run. Switching to a double linked list which makes both insert and delete O(1) made it run in 17 seconds. Still pretty slow but a vast improvement
1
u/[deleted] Dec 10 '18
I'm at slightly below 100 milliseconds for part 2 (7,192,000 rounds) with a plain-C implementation, or 150 milliseconds if you include the compile time (GCC 7.3 on Core i7-4790K).
https://github.com/kajott/adventofcode/tree/master/2018/09
1
u/14domino Dec 10 '18
It's annoying how long it took me to come upon the a-ha moment: use a doubly linked list! This was after a while for waiting for a solution to part 2. I implemented my own in Python and forgot that deque existed. If you read the problem, there are many hints that you should use a linked list, starting with the problem statement that the very first marble is also a circle of marbles, and the weird way he worded inserting a marble between x + 1 and x + 2 (instead of just saying two spaces after). Etc.
1
u/Marutk Dec 10 '18
5 seconds using this data structure http://hackage.haskell.org/package/containers-0.6.0.1/docs/Data-Sequence.html
1
u/rebelcan Dec 10 '18
In Go:
Code is here if you're curious.
1
u/minimallymist Dec 10 '18
I solved it in Julia and used a linked-list like structure implemented in an array. The idea is that you can use an array where each row is an entry of a linked list where the first column points to the entry before and the second to the column after. Thus, to see which marble lies next to marble number 4, you simply lookup
array[4,1], array[4,2],giving you the previous and next marble respectively.Knowing how many marbles you place overall you can make your array large enough to handle all of them. The removed marbles will be wasted space in your array but given the performance and memory requirement that's no problem. (This is of course highly specific to all inputs being consecutive integers and it being relatively dense).
The 1-based indexing was a small drawback here but I still enjoyed it a lot.
With that, the first run of both together (which includes compilation of the function and usually isn't considered part of the runtime) takes 120ms. Second run (already compiled) of both takes 90ms and using the
BenchmarkToolssuite, I get 64ms runtime. My solution:1
u/o11c Dec 18 '18
63 seconds in python3 using
blist, with code written in a very straightforward manner.Not worth spending more programming time optimizing it, I'm enough days behind as it is.