Learn Programming with Kotlin

Henrique Rocha
6 min readNov 3, 2022

--

In this article you’ll learn programming with Kotlin from scratch.

To guide the learning process we will solve a programming problem from the Advent of Code. We will be using the first problem from the 2015 edition of Advent of Code. Read the problem first and then continue reading here.

It might seem very overwhelming to you if you never programmed before but I believe it will be easy to understand.

The first thing you will have to be mindful of is that you will gradually have to acquire the skill to be able to ignore the text that doesn’t add any kind of value to the problem statement. For example, we don’t care that Santa was hoping for a white Christmas and we also don’t care that he’s trying to deliver presents in a large apartment building.

Let’s have a look at what really matters…

He starts on the ground floor (floor 0) and then follows the instructions one character at a time.

These instructions are just a sequence of characters given in our problem input that you can download from the page. You see that it’s just a sequence of parenthesis. This means that we will have to somehow learn how to process that file.

Another important part of the problem statement is the following:

An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.

How can we represent this “go up one floor” and “go down one floor”? And how do we know in which floor we currently are?

Variables

A programming concept that you’ll use over and over again is the concept of variables. Basically a variable is just a labelled box where you can store things. In the case of Kotlin, the type of the things you put in that box is also attached to that box. In this case the box would be named floor for example and its type would be a number. In this specific case, Kotlin represents that number as the type Int. Since the problem statement mentions that Santa starts on floor 0, this would be defined in Kotlin like this:

var floor = 0

Another thing that we also get from the problem statement is that we will have to distinguish between opening and closing parenthesis. This concept is handled by conditional expressions.

Conditions

If the instruction that we read is an opening parenthesis we go up, if it’s a closing parenthesis we go down. This “if” that I used in the beginning of the sentence is exactly the same that you will use in Kotlin. The condition that we want to check will be written between parenthesis and the action comes in front. Like this for example:

if (instruction is an open parenthesis) go up one floor
if (instruction is a closing parenthesis) go down on floor

Unfortunately we can’t write it like that. Let’s take care of that “instruction is…” first.

Comparisons

To compare if something is equal to another we use the == operator. Yes, two equal signs. The instruction would actually be another variable that we would have read from our input (we’ll learn how to do that later) and the open parenthesis is a character that in Kotlin would be represented by ‘(‘, the open parenthesis in single quotes. Single quotes are used to tell Kotlin that we’re dealing with a single character. Our if condition would be written like this:

if (instruction == ‘(‘) go up one floor
if (instruction == ‘)’) go down one floor

Arithmetic expressions

In Kotlin we can do arithmetic expressions, which we have to use in this case to be able to go up and down floors.

If we are on floor 3 and we go up one floor we will be on floor 4. What did we do? Simply incremented the floor value by one, which can be done in Kotlin by writing floor + 1

The problem here is that we actually want the result of that expression to be the new floor so we have to update the value of our variable floor.

Assignments

In Kotlin assignments are done with the equal sign. This might be confusing to you at first because it doesn’t represent equality but assignment. Remember that equality is represented by two equal signs.

variable = value

This means that the box named variable will now contain the value on the right. In our case we would do floor = floor + 1. This means, get the current value of floor, add one to it and store it in the same place, basically overriding whatever value was there before. Since this is so common to do, there is a shorter version that you can use, which is floor += 1. Or even floor++. Use whatever is more understandable for you for now.

Of course if we can increment we can also decrement. I’ll let you figure out by yourself how that would be.

Input and output

Programming is all about processing an input and producing an output. In this case the input is given with the problem statement and you can download the input.txt file provided but we will test our algorithm first in a simpler way. An algorithm is just a sequence of steps we will perform to produce our desired output.

Our input is a sequence of characters, this is called a string in the programming world. From now on I’ll use the term string instead of sequence of characters.

Strings

Instead of reading from a file, which is a more complex topic we will address in a future article, let’s just use a variable that will contain our input. A string is just a sequence of characters between double quotes, for example “(())”. We could have a variable that contains this string like this:

var input = “(())”

Values

In this particular case our variable should actually to be a variable because it won’t change. When you know that the value that is in the box will not change, you define that box with val instead of var. The correct way to do this would be like this:

val input = “(())”

Of course using var would also work but you should right from the beginning get the mindset of being able to identify things that will change or not change. This will give you some guarantees that will help you in the future when you learn more about programming.

Now that we have our string how do we go through it? We need the concept of looping.

Loops

The are multiple ways of looping. The one we will learn is very simple. It allows us to express something like “for each character in our input, do something”

val input = “(())”
for (instruction in input) do something

What this will do is go through our input string and step by step put each character in the instruction variable. The “do something” would actually be our if conditions that we mentioned before because we want to go up and down depending on the instruction.

It would be like this:

val input = “(())”
var floor = 0

for (instruction in input) {
if (instruction == ‘(‘) floor += 1
if (instruction == ‘)’) floor -= 1
}

Notice the curly braces, they are used to form a block and whatever is inside this block will be executed against each character that we read from the input.

How do we execute all this and get the result?

The main function

In Kotlin each program starts its execution in a function called main. This is just this:

fun main() {
instructions here
}

fun is obviously the abbreviation of function. Those open and closing parenthesis after the word main mean that this function doesn’t get any input but you don’t need to worry about that now.

Showing output

Now that you know about functions, there is another very useful function called print. You can pass a value or variable to this function and its value will be displayed on the screen. For example print(floor). Notice the usage of parenthesis. Usually we do a line break after printing a line so we would use another function called println. The ln is the abbreviation for line.

Full program

Our full program would look like this:

Running this code would display the value 3 on the screen.

You can try it in the Kotlin website kotlinlang.org in the Try Kotlin section. Write your code there and click Run.

Feel free to experiment with what you’ve learned. You can copy-paste the string from the input.txt file and run the program and you will have the right answer that you can submit to complete the problem.

In the next article you will learn how to process the file directly.

If you reached this point, thank you very much. This is my first article on Medium. If you liked this tutorial feel free to support me on Patreon.

--

--