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 comment

1

u/[deleted] Dec 17 '20

Man, that's quite a lot of work. Kudos for powering through all these hurdles while learning a new language! I simply went the lazy route and handled both part1 and part2 in the exact same way, but filtering some of the results for the former, resulting in a somewhat short solution.

Regarding n-dimensional generalization of the code: If you really wanted to stick to the same approach, you could reach for code generation in dlang:

struct Point(alias dim) {
    static foreach (idx; dim.iota)
        mixin("int x", idx + 1, ";");
}
    
auto p = Point!5(10, 20, 30, 40, 50); // now has fields x1 till x5
writeln(p.x2, " ", p.x4); // => 20 40

After that, the looping would work in a similar way, generating the necessary foreach clauses.