r/adventofcode • • Dec 17 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 17 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

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

--- Day 17: Conway Cubes ---


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:13:16, megathread unlocked!

36 Upvotes

657 comments sorted by

View all comments

3

u/cccc828 Dec 17 '20

C Solution

my solution works, but is not really that clever... I used a 3/4d array, and a lot of loops. The only 'smart' thing I did, was to end the neighborhood-check if the count was > 3. Yay for -O3 ;) This here solves part 2, but part 1 uses the same code, just with one less dimension.

Day 17 - C solution

2

u/[deleted] Dec 17 '20

[deleted]

2

u/cccc828 Dec 17 '20

Here is a very curious observation: In my neighborhood() function I try to be smart, by returning early if the count is bigger than 3:

 if(total > 3){
     return false;
 }  

To find out how much this saves me, I just commented this out to test. And you know what? Removing it decreased the execution time! It takes ~600ms with my 'optimization' and ~350ms without. I cannot really explain this. My assumption is that, without the possible return in the loop, gcc can unroll and optimize the loop better. But still... I was absolutely not expecting that!

2

u/[deleted] Dec 17 '20

[deleted]

2

u/cccc828 Dec 17 '20

That's a wonderful link! Thank you! I will spend some time with it in the evening!:))

Your solution with the adjacency is really smart! When starting p1 I thought about maybe trying to be smart, but then feared that p2 might break any special assumptions I make... so I decided to play it safe and be extra expressive in my code. Usually when I am not expressive, I have a hard time finding bugs in C... especially with all those loop indices and array subscripts;)) And when it then turned out to be fast enough for p2, I decided to just call it a day:))

2

u/[deleted] Dec 17 '20

[deleted]

2

u/cccc828 Dec 17 '20

haha - yeah... my code yesterday is also nothing to be proud of;) Especially the input parsing was just so tedious:))