🍩.🍇 (donut.c, but Emojicode)
Tags: donut, emojicode, challenge, esoteric
Published: 2023-10-17 20:00:55

Inspired by [the work of Evan Zhou](https://www.youtube.com/@evanzhoudev), I determined that the world needed a `donut.c` variant with A LOT more sprinkles... thus `🍩.🍇` was born.

`🍩.🍇` is written in [Emojicode](https://www.emojicode.org/), a language that makes as much use of emoji as possible. I have used Emojicode for several years to solve [Advent of Code](https://adventofcode.com/2023/about) coding challenges and have found it simultaneously frustrating and delightful to code in. Naturally porting `donut.c` to Emojicode was an enticing idea.

## Coding Difficulties

It took me about 6 hours to port the original C code to Emojicode, as I ran into some snags along the way:

### [Compiler bug](https://github.com/emojicode/emojicode/issues/217)

In theory, these two lines from the original `donut.c`:

```c
printf("\x1b[2J");
printf("\x1b[H"); 
```

should translate to the following Emojicode:

```emojicode
😀🔤❌e[2J🔤❗
😀🔤❌e[H🔤❗
```

Unfortunately the compiler complains about an unrecognized escape sequence. Thus, I wasn't able to include these lines (which clear the terminal and move the cursor to the top of the terminal). So if you run my program, it will just keep drawing more and more lines without clearing them. You need to size the terminal just right to avoid seeing more than one frame at once.

### Shaping the code as a 🍩

Once the code was translated from C and working, the next challenge was to get the code shaped like a donut. Although Emojicode is not whitespace-sensitive, I discovered immediately that it wouldn't be as simple as putting line breaks every N characters.

Normally, code displayed in a monospace font has every column line up nicely, regardless of the relative width of the letter:

```plaintext
Hello
World
```

But emoji aren't compatible with that paradigm.  Emoji apparently are supposed to take up the width of two characters in monospace contexts, but depending on your browser, operating system, device, etc. that will vary. The following two lines contain four normal characters and three emoji characters, but do not line up (at least they don't for me, but maybe they do for you 🤷):

```emojicode
0.0➡️🖍️🆕i
↪️22▶️y🤝y
```

This lack of consistency made shaping the code very challenging as I couldn't use any ASCII donut templates to fill it in.

One more thing that caused a lot of difficulty with shaping the code was the one string literal in the code, used as a gradient for drawing darker and lighter "pixels" of the donut:

```emojicode
🔤.,-~:;=!*#$@🔤
```

As Emojicode no longer has a string concatenation operator, I was constrained to keep all these characters from spanning multiple lines. This forced all the code above this string to be a certain size, which also determined (to some extent) the size of the code below it.

In the end, I was able to fit all the code into a donut shape, without adding any superfluous spaces or comments, which surprised me!

## The final product

```emojicode
                🏁🍇0.0➡️🖍️🆕A
            0.0➡️🖍️🆕B🔤 🔤➡️_🆕🍨🐚
         💯🍆A 1760❗➡️🖍️🆕z🆕🍨🐚🔡🍆_
        1760❗➡️🖍️🆕b🎶🔤.,-~:;=!*#$@🔤❗
     ➡️L🔁👍🍇🔂n🆕⏩0 1760❗🍇_➡️🐽b n❗0.0
    ➡️🐽z n❗🍉0.0➡️🖍️🆕j🔁j◀️6.28🍇0.0➡️🖍️🆕i🔁
   i◀️6.28🍇📓i❗➡️c📕j❗   ➡️d📓A❗➡️e📓j❗➡️f📕A
  ❗➡️g d➕2➡️h 1.0➗          🤜c✖️h✖️e➕f✖️g➕5🤛
 ➡️D📕i❗➡️l📕B❗➡️m              📓B❗➡️n c✖️h✖️g➖f✖️
 e➡️t🔢40.0➕30.0✖️                D✖️🤜l✖️h✖️m➖t✖️n🤛
 ❗➡️x🔢12.0➕15.0               ✖️D✖️🤜l✖️h✖️n➕t✖️m🤛
  ❗➡️y x➕80✖️y➡️o🔢            8.0✖️🤜🤜f✖️e➖c✖️d✖️g
   🤛✖️m➖c✖️d✖️e➖f✖️g➖       l✖️d✖️n🤛❗➡️N↪️22▶️y🤝y▶️
    0🤝x▶️0🤝80▶️x🤝D▶️🐽z   o❗🍇D➡️🐽z o❗0➡️🖍️🆕
     I↪️N▶️0🍇N➡️🖍️I🍉↪️I▶️🙌📏L❓🍇📏L❓➖1➡️🖍️I
      🍉🐽L I❗➡️🐽b o❗🍉i⬅️➕0.02🍉j⬅️➕0.07
         🍉🔂k🆕⏩0 1761❗🍇 ↪️k🚮80🙌0🍇
           😀_❗🍉🙅🍇👄🐽b k❗❗🍉🍉
             A⬅️➕0.04 B⬅️➕0.02⏲🐇
               🧵 10000 ❗🍉🍉 
```

To run this: 

* follow the [Emojicode setup instructions](https://www.emojicode.org/docs/guides/install) for your OS
* paste the code into a file (I use the filename `🍩.🍇`, but you could use `donut.emojic` for the same results)
* compile the code: `emojicodec 🍩.🍇` (or `emojicodec donut.emojic`)
* run the program: `./🍩` (or `./donut`)

![🍩.🍇 at runtime](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqhj1hv3i0kchana014g.gif)

## Conclusion

Overall, this was a pretty fun project, and I'm happy with the results. Go join Evan Zhou in his quest to compile a donut code file for as many languages as possible: <https://github.com/EvanZhouDev/TheDonutProject>