My polyglot Advent of Code

Signal v. Noise

At Basecamp we have an internal project called “Your proudest moments”. My colleague Dan (https://m.signalvnoise.com/author/dan-kim/) set it up so that people at Basecamp could share anything we’re proud of. So far people have shared impressive, really feel-good accomplishments, such as performing complicated house renovations without professional help, writing books, or taking their parents on an unforgettable vacation.

This post comes from my first contribution to this project. As I told them, it went to “Your proudest moments” because we don’t have a “Your most useless and pointless self-inflicted programming hours” project, that would have been the best fit. Still, this quite a ridiculous thing to do made me super proud, and I also had a lot of fun doing it.

All the 50 stars!

Advent of Code (https://adventofcode.com/2019/about)  is an advent calendar of programming puzzles that’s been happening since 2015, made by Eric Wastl. (http://was.tl/) Every day from 1st to 25th December a new puzzle with 2 parts gets released and for each part solved you get a star. The goal is to collect all 50 stars to save Christmas. All the problems follow a story normally involving the space, a spaceship, elves, reindeers and Santa. The difficulty increases as the days pass. First ones are simpler, but then they start getting complicated and laborious. Some of them are pretty tricky! They aren’t necessarily super hard algorithmically, binary search, BFS, Dijkstra, Floyd–Warshall, A*… might be all you need but I can easily take several hours to finish each one. This year there was also a bit of modular arithmetic that I loved and a little bit of trigonometry. The problems are super cool. This year for example included things like a Pong game in Intcode, a made-up assembly language (you had to program the joystick movements and feed them to the program) and a text-based adventure game in Intcode as well. It’s seriously cool.

the quality and thought behind the problems in AoC is really outstanding, you can feel the amount of work and dedication behind their preparation, and the result is challenging, engaging, and fun (CAVEAT: marriages may be harmed) https://t.co/CWnSgfTsTh

— Xavier Noria (@fxn) December 12, 2019 (https://twitter.com/fxn/status/1205090366984261632?ref_src=twsrc%5Etfw)

I’ve done it in 2016, 2017 and 2018, the first time in Ruby, then the last 2 years in Elixir. I never finish on the 25th December because for me it’s impossible to work, take care of life stuff and also spend several hours programming these puzzles every day 😅I normally finish around 27th or 28th December. This year I was moving to a new apartment in the middle of December, and since that wasn’t stressful enough, I took on a new challenge with Advent of Code and finished just this Sunday: doing a polyglot version, that is, every day in a different programming language! I was inspired by a friend who had done this in 2018. I thought I’d give up, but the more I solved, the more invested I was and the less willing to give up!

This is the list of languages I ended up using:

Day 1 in awk (http://www.awklang.org/)

Day 2 in Prolog (https://www.swi-prolog.org/) (SWI)

Day 3 in Haskell (https://www.haskell.org/)

Day 4 in Common Lisp (https://common-lisp.net/)

Day 5 in Kotlin (https://kotlinlang.org/)

Day 6 in Swift (https://docs.swift.org/swift-book/)

Day 7 in Lua (https://www.lua.org/)

Day 8 in Perl (https://www.perl.org/)

Day 9 in Go (https://golang.org/)

Day 10 in C (http://csapp.cs.cmu.edu/3e/docs/chistory.html)

Day 11 in Rust (https://www.rust-lang.org/)

Day 12 in OCaml (https://ocaml.org/)

Day 13 in Julia (https://julialang.org/)

Day 14 in Erlang (https://www.erlang.org/)

Day 15 in Dart (https://dart.dev/)

Day 16 in Dylan (https://opendylan.org/)

Day 17 in Scala (https://www.scala-lang.org/)

Day 18 in Python (https://www.python.org/)

Day 19 in Crystal (https://crystal-lang.org/)

Day 20 in Nim (https://nim-lang.org/)

Day 21 in Ruby (https://www.ruby-lang.org/en/)

Day 22 in R (https://www.r-project.org/about.html)

Day 23 in Elixir (https://elixir-lang.org/)

Day 24 in C++ (https://isocpp.org/)

Day 25 in JavaScript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Introduction)

Every year there’s some sort of made up assembly code that you have to write an interpreter for, and then it reappears in subsequent problems, so you reuse your code, enhance it, etc. In the past, there had been a handful of problems using this assembly language. This year, however, 12 problems involved Intcode programs (the name of 2019’s made up assembly). Doing a polyglot challenge meant I had to rewrite the interpreter every time. I almost go mad – .. hysterical laughter 😆. 

A very important part of the challenge for me was trying to write code as good as possible, even for languages I had never seen. I didn’t want to do complete problems by learning how to declare variables, loops and if-else and force my Ruby or Elixir solution into the language. This meant looking at style guides so I could follow the naming conventions and that stuff, but also getting familiar with idioms and structures, and looking at official repos and examples if possible, so my code was at least a little idiomatic. This took a lot of time in some cases, and I’m not sure I achieved my goals, but I did my best! What I didn’t do was to learn and use some stuff from some languages as I couldn’t really fit it in these kind of problems (like macros to manipulate AST nodes in Crystal or Nim, concurrency stuff in Erlang…).

Some fun things I learnt and other anecdotes from doing this challenge:

* Programming languages with arrays starting in 1 are stressful. In my case, this was Lua, R, Julia and Scala. Off-by-one errors are twice as fun here. 

* Rust is as cool as I had imagined it to be, but it has this complex and interesting memory management system that I only scratched the surface of. Ownership, borrowing and memory safety. Errors were ridiculously cryptic if you didn’t understand this very well, and yes, the hour I spent looking into it wasn’t enough to understand it well. 

* I always forget how much I like Golang and this made me remember. It’s so neat.

* Erlang was such a nice surprise. I had never used it and had this unfounded idea it was tough and unpleasant. It’s not, it’s lovely! ❤️ This must be a false rumour spread by Erlang programmers so they can remain in their exclusive club.

* Julia was a nice surprise, easy to pick up, at least for the basics, and I imagined it being pretty good for scientists.

* I also remembered how much I love C, it was the first language I learnt. I’m super grateful I don’t write C code professionally, though. It’s seriously scary. One obscure, edge error and someone will get root in your machine.

* I used Kotlin and Swift too soon for too simple problems! Same for Common Lisp and Prolog. I wasted them when I still thought I wouldn’t finish the challenge. 

* I skipped PHP and Java! Two languages I know and have even used professionally, but dislike them so much that I preferred to learn new ones! Enough PHP and Java in my life.

* Nim was a language I didn’t even know it existed. So glad I chose it.

* Crystal’s syntax is so similar to Ruby that I felt I was cheating by using it in the challenge. Still, I made the rules, so…🤷🏻‍♀️

* Dylan was impossible to find documentation and examples for, in the regular ways programmers search for stuff nowadays, at least. Whenever I’d google how to do something, all I’d find was someone named Dylan explaining how to do whatever I wanted to do in another language like Java. I ended up using a really good book from the 90s that I found available online in PDF. 

* Naming conventions in R are famously anarchic (https://www.r-bloggers.com/consistent-naming-conventions-in-r/) ! 5 naming conventions to choose from, and multiple conventions used simultaneously in the same packages, style guides and tutorials 🤣In my code I went for the period.separated one: e.g. cut.cardssimply because I wasn’t going to have the chance of using this convention in any other language. Of course, I threw in some naming inconsistencies as well to comply with the chaos! ✌️

I hope you enjoyed reading this. If you’re interested, here are all my solutions (https://github.com/rosa/advent-of-code/tree/master/2019)  for 2019 and the previous years. I’d be forever grateful to Eric Wastl for creating Advent of Code, and I’m sure many other programmers share this gratitude. Now I need to think of a new challenge for next year!

0件のコメント

著作権

この記事は Signal v. Noise が提供するRSS フィードを表示したものです。 元記事はこちらです。

リストへ戻る
+
+