Advent of Code 2020 – My foray into Scala
This year I tried to do the advent of code again, and I got to day 13 this time. Not because I got stuck, it is mainly due to time management issues.
The code I wrote is on this repo here. I chose the name because I liked the autogenerated one GitHub gave me. Also I am not showing this repo because I want you to view some epic god-like tier code that happens to be Scala, no. It is just to show you what you can do with Scala and how it seems to fit these kind of problems really really well.
Perfect fit
When I started the problems are really simplistic, like doing some mathematical operations on a list of numbers or processing matrices. Some of the earlier ones can be solved naively and in a brute-force kind of way. Later on that won't work anymore as the inputs will be too big to compute or hold in memory on most devices.
For these kind of problems where a lot happens on lists of things, that is usually the input, Scala just offers a variety of tools to operate on these lists. For instance the .sliding()
or .grouped()
operations. What they essentially do is go over a list and per whatever size and step size you choose give you back the elements. For instance the list [1,2,3,4,5]
with sliding(3,1)
operating on it will give back:
[1,2,3]
,[2,3,4]
,[3,4,5]
This operation is very useful in these situations as most often you have to compare the neighbouring elements. So you sort a list, do a sliding operation and then some functional mapping and you get the answer.
Nice structures
The structures arising out of Scala code is one I really like. There exist a couple of concepts and I will only talk about the ones I used. The first is a object
. It is similar to a Singleton
class idea in normal Java. It holds static methods and variables. This is not to say you cannot have a variable in there outside of methods but it is not usual.
Then exists the normal class like everywhere else. However you can combine this class with an object to create a hybrid. You can therefore apply logic in a nice way and keep everything separated. Then exists the case class
concept where you have a very minimal class with just property accessors. You can have these properties be immutable (default) or mutable (use the var
keyword in front of it). They can be likened to the new Record
concept in Java that also exists in other languages. The case class
can have one or more methods, but preferably not.
Then there also exists the concept of a case object
. This is super minimal one that does not even take in parameters and holds anything. So why would you use this? Well for example in enumeration it is really helpful or when passing messages. When you want to easily pass around an object that conveys what needs to be done. I used this to represent the cardinal directions. Then I could easily pass around these and use them to implement some logic.
Concurrent programming
This is by far the easiest to do in Scala than compared to Java. You just wrap it in a Future and have an implicit executor. What does it look like? Like this:
def line_of_sight(layout: Array[Array[Seat]]): Unit = {
var futures : List[Future[Unit]] = List.empty
futures +:= Future { north(layout) }
futures +:= Future { north_east(layout) }
futures +:= Future { east(layout) }
futures +:= Future { south_east(layout) }
futures +:= Future { south(layout) }
futures +:= Future { south_west(layout) }
futures +:= Future { west(layout) }
futures +:= Future { north_west(layout) }
ConcurrencyUtil.awaitAll(futures)
}
This is taken from day 11. That is it. Now all those functions will be concurrent.
I hope I will find a project soon where I can use this language to its full potential.