Yesterday was a big one! I ended up just writing and writing and writing and looping and looping and looping. And, if I'm being totally honest and transparent, my original solution before refactoring had a goto
in it. Not great. But it came out pretty good after some refactoring and pulling logic out into functions.
The Puzzle
In today’s puzzle, we're back to Conway. This time in the third dimension. We've got cubes that turn on and off depending on how many of their 26 neighbors are also active. And the rules for transition are pretty low, so I think this is going to be one blinky cube!
The Leaderboards
As always, this is the spot where I’ll plug any leaderboard codes shared from the community.
Ryan's Leaderboard: 224198-25048a19
If you want to generate your own leaderboard and signal boost it a little bit, send it to me either in a DEV message or in a comment on one of these posts and I'll add it to the list above.
Yesterday’s Languages
Updated 02:04PM 12/17/2020 PST.
Language | Count |
---|---|
Haskell | 2 |
JavaScript | 2 |
Rust | 2 |
C | 1 |
Ruby | 1 |
Merry Coding!
Top comments (10)
Tried for ages to get a generic solution for an dim size, but in the end failed in the most part! Anywhy here's a Haskell soution:
I did mine in Python. Not enough brain this week to write all those loops in C. Part 2 isn't very exciting because it's exactly the same thing with one layer more of nested looping for everything. I had to just turn my brain off and write the loops, or my brain would have eeped out of my ear.
As with my previous cellular automata, for this problem, I decided to just let TensorFlow handle the heavy lifting of a 4D convolution. However, TensorFlow only has 3D convolution (which I used for the first part) so I also had to write/adapt a 4D convolution script to handle it.
It's very fast though, running on the GPU
With hindsight, a more dynamic approach of using a variable-length vector for a coordinate would have been better, but it was a chance to play with the Rust type system if nothing else.
It's funny how (at least some of) the solutions are similar to each other, even if written in different languages. Here's my Perl solution:
My Haskell solution. I created a matrix the size of the input + 7 in each direction and dimension. So the input matrix is in the very middle. Since on 6 cycles it can only grow a maximum of 6 in each direction, I chose 7 because then I can get away with not doing bounds checking. I'm not looping over the outer layer anyways. I also got to use mutable arrays in Haskell, which I recently learned how to use. The part 2 solution was just a copy-paste and adding an extra coordinate. Although I could just have made part 1 take a fourth coordinate but keep it at 0. I also learned that Haskell arrays can handle negative indices just fine, so it was easy to just set the input array at index 0 and then just set the array to be to -7.
Ruby solution. It's an eternity of
each
s (Ruby'sfor
loop), but it runs in a few seconds.Today's main problem was the example. Instructions were pretty clear, but the example had me thinking I misunderstood. Had to check Reddit, where a lot of other folks were confused too, and realised I was correct, the example had just been cropped weirdly.
Clarification request
we are given:
Lets put some coordinates around this and expand it so I can easily refer to things
Now concentrate on the initial state, before any cycles: the cell at x=C, y=2 , z=0 or C,2,0 for short.
It is bottom-right. It has two active neighbours C,1,0 and B,2,0 That should mean that it stays active in cycle 1 but is shown inacative.
The rule that applies is:
That first cycle does not seem to fit the task explanation.
Could someone explain how they understand the explanation please, Thanks.
P.S. C,2,0 is marked 'X' below to help explain the coordinates
This is confusing to look at too, but there's one important phrase that makes it correct:
This thread, makes it more clear. At time t=1, the possible grid is actually 5x5x3. The sample input is just shifted and shrunk to follow the boundary of actual cells.
In other words, for your diagram's time step 0, cell 2C is the same cell as cell 1, C, 0 in time step 1. Try it out yourself but fill it in in a 5x5x3 grid. Let me know if this makes sense! I agree that it's confusing and he would have probably been better off showing the whole potential state space and not shifting to follow the active cells. :)
My JavaScript walkthrough:
4D messed with my head but it ended up ok 😅