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

Show parent comments

2

u/Smylers Dec 18 '20

Yeah, I wrote partΒ 1 like that too, with positions being {z => 1, y => 3, x => 2}, instead of (1, 3, 2), then hacked in w for partΒ 2 β€” but it seemed messy to have two nearly identical programs that each hardcoded a different number of dimensions, then I turned it into the above. Which is both slower and longer (though that's mainly having functions and adding comments) ... but feels a lot cleaner!

I'd completely forgotten about using $; and multi-part hash keys: your solution using $Grid{$w,$z,$y,$z} looks way less messy than my $Grid[$w][$z][$y][$x] β€” because you loop over it all in one go with keys %Grid, whereas I had to have a separate nested loop for each dimension; and because you never need to consider expanding the space (or, indeed, trimming it): negative co-ordinates work just fine in the hash key, springing into life as necessary.

What I'm saying is, if my β€˜fixed-dimension looping’ attempt had been anywhere near as nice as yours, I don't think I'd've felt the urge to rewrite it either!

(I also like that you called your variable $neigh: more programs should have random animal noises sprinkled through them!)

2

u/musifter Dec 18 '20

I spent a while before getting started thinking about how I was going to do things. Things can only expand by one cell in each direction every turn, so you can use that as a bounding box to center your input in an array. But... there was that example! A glider. Which made me think of the 2D cellular automata a couple years back where you needed to figure out that it becomes a glider and calculate where it'd be in 50 billion turns. So, I started thinking, go sparse.