Diamonds: It's Life Jim, but not as we know it

After posting some short C code to solve the Diamond kata (see https://www.dhirubhai.net/pulse/diamonds-c-carlo-pescio-byfbf/), I couldn't help asking myself, "What would a good object-oriented solution look like?" (Keep reading even if you don't care about objects; there is a plot twist.)

As you may know, Alan Kay took some inspiration from biology when creating Smalltalk. So, I thought it would be cool to create a world populated with cellular automata, like in Conway's Game of Life (but with different rules), where a single "A" placed at the center would evolve, through several generations, into a diamond like in this picture:

The trick, of course, was to find rules that not only worked but were "nice enough." At that point, being object-oriented became sort of irrelevant (I already know how to create this kind of thing in an OO style), and I focused on finding the rules instead. Spoiler: it worked decently.

For simplicity, I created a two-phase process: an anabolic phase, in which cells replicate, and a catabolic phase, in which cells die. Each phase creates a new world, and cells spawning from the existing world will try to enter the new world. This is useful because, if you do it in place, the result of the simulation depends on the scanning order (which sucks), or you have to keep a buffer (which still sucks). The world is sized depending on the size of the diamond we want: when cells reach the border, the process stops.

In the anabolic phase:

- A cell splits into 4 cells: two identical cells will try to enter the spaces above and below the cell, and two "next-gen" cells (so A -> B, etc.) will try to enter the spaces left and right. NB: the cell splits, so it won't move to the new world. You can visualize it like this:

When a cell X enters a space in the new world:

- If it's empty, it fills that space as in the picture above (so: ? + X = X).

- If there is already another cell with the same letter, they merge into one (so: X + X = X).

- If there is already another cell with a different letter, they form a bundle (I used '*' to show bundles, so: X ≠ Y => X + Y = '*' ; that includes the case where Y = *).

The catabolic phase is easier: the bundles die. That's it. From this rule, it follows that in the anabolic phase above, X is never *.

You can see in the video how generations follow each other. Of course, the process is artificially slowed down, with a pause of 600ms between each phase.

Now: I'm not telling you this is the best solution. It's something I wanted to try out because I'm a nerd and I cannot help it. Also, it proves once more that (as I always say) the design space is way larger than most people dare to explore.

A few notes on the overall process:

- To prototype the idea, I used the best tool available on Earth: the spreadsheet.

- The code is in C# but could have been in any language.

- I just sat down and wrote it, and it worked because I'm a Super Saiyan. You probably want to use a different approach, which is fine: I'm not selling you anything.

As an aside: the title is a cornucopia of inside jokes, but that's the exercise for the reader.


Matteo Vaccari

Technical Principal @ Thoughtworks Italia

8 个月

Hi Carlo you mention a video but I don’t see a link

回复

要查看或添加评论,请登录

Carlo Pescio的更多文章

  • The Bank OCR Kata, part 2: Hamming distance to the rescue

    The Bank OCR Kata, part 2: Hamming distance to the rescue

    In my previous post (link in the first comment as usual) I addressed the first part of the kata: character recognition…

    1 条评论
  • A compact, highly efficient solution for the Bank OCR Kata

    A compact, highly efficient solution for the Bank OCR Kata

    The Bank OCR Kata (link in the first comment) is a simple but non-trivial programming exercise. The problem statement…

    6 条评论
  • Harry Potter and the Magic of Common Good

    Harry Potter and the Magic of Common Good

    The Harry Potter kata (https://codingdojo.org/kata/Potter/) is a simple optimization problem: there is a series of 5…

  • Diamonds in C

    Diamonds in C

    A few days ago I was briefly involved in a discussion about the Diamond Kata (see https://blog.cyber-dojo.

    1 条评论

社区洞察

其他会员也浏览了