r/adventofcode • u/askalski • Jan 14 '19
Upping the Ante [2018] Optimized solutions in C++ (41 ms total)
According to the About page:
Nor do you need a fancy computer; every problem has a solution that completes in at most 15 seconds on ten-year-old hardware.
Hereby establishing a tradition of keeping u/topaz2078 honest, I have completed my second annual set of optimized solutions, this time clocking in at just under 41 milliseconds to solve all puzzles.
The C++ code is on GitHub. All solutions are single-threaded, but make extensive use of SIMD techniques and therefore require a recent Intel-compatible processor with support for the AVX2 instruction set.
As always, if you have a question or are curious about how a solution works, please ask.
Timings by day (i7-7700K mildly overclocked, with a turbo frequency of 4.60 GHz):
Day 1 72 μs
Day 2 27 μs
Day 3 205 μs
Day 4 82 μs
Day 5 414 μs
Day 6 1,346 μs
Day 7 3 μs
Day 8 62 μs
Day 9 4,385 μs
Day 10 21 μs
Day 11 485 μs
Day 12 61 μs
Day 13 642 μs
Day 14 19,067 μs
Day 15 2,132 μs
Day 16 80 μs
Day 17 544 μs
Day 18 307 μs
Day 19 2 μs
Day 20 92 μs
Day 21 101 μs
Day 22 3,546 μs
Day 23 383 μs
Day 24 6,711 μs
Day 25 177 μs
------------------
Total 40,959 μsSee also my optimized solutions for 2017.
12
u/askalski Jan 15 '19
Small programming challenges like the Advent of Code puzzles are a great way to practice. Start with an easy puzzle, and challenge yourself to make it run as fast as possible. When you think you've squeezed every last cycle out of your code, see if you can make it go even faster. Don't be afraid to break rules. If a case doesn't come up in the puzzle inputs, don't handle it. Nobody's going to make you write up a CVE disclosure for your AoC solutions.
A few resources that I have found helpful:
Bit Twiddling Hacks
The book Hacker's Delight by Hank Warren
The Intel Intrinsics Guide, aka "There's an instruction for that?"
Anything from Agner Fog, especially the Instruction Tables
The Art of Computer Programming by Donald Knuth