<![CDATA[Advent of Code 2021]]>https://triozer.github.io/aoc-2021-in-kotlin/https://triozer.github.io/aoc-2021-in-kotlin/favicon.pngAdvent of Code 2021https://triozer.github.io/aoc-2021-in-kotlin/Ghost 4.25Fri, 03 Dec 2021 10:12:24 GMT60<![CDATA[Day 2. Dive!]]>Introduction

Today's have a ton of possible solutions! Let's watch two ways in this post. The first one will be "the logical" way while the second will be more focused on Kotlin and functional programming.

Problem Input

Now our submarine can take the following

]]>
https://triozer.github.io/aoc-2021-in-kotlin/blog/day-2/61a95c7554e62874979e3f11Thu, 02 Dec 2021 22:59:00 GMTIntroduction

Today's have a ton of possible solutions! Let's watch two ways in this post. The first one will be "the logical" way while the second will be more focused on Kotlin and functional programming.

Problem Input

Now our submarine can take the following commands forward, down, up.

First, we will have to parse the list of received commands. To do this feel free to look at how it is done in the Utils.kt file.

Version 1

Part 1

Problem

Here the code is kinda straight forward. There is no much to explain. Maybe we can talk about array using destructing.

So, in Kotlin (as in JavaScript, Python...), we can easily destructuring an array / object. Given we have the following array arr = [1, 2, 3], if we want to access these values, we should use their indexes:

val arr = [1, 2, 3]

val a = arr[0]
val b = arr[1]
val c = arr[2]

Whereas using destructuration, we can have:

val (a, b, c) = [1, 2, 3]

This is exactly what I've done for splitting the command and it's value.

Solution

fun part1(input: List<String>): Int {
    var height = 0
    var depth = 0

    for (line in input) {
        val (command, step) = line.split(" ")
        when (command) {
            "forward" -> height += step.toInt()
            "down" -> depth += step.toInt()
            "up" -> depth -= step.toInt()
        }
    }

    return height * depth
}

Part 2

Problem

The second part only add a new variable, but the main "logic" is still the same.

Solution

fun part2(input: List<String>): Int {
    var height = 0
    var depth = 0
    var aim = 0

    for (line in input) {
        val (command, step) = line.split(" ")
        when (command) {
            "forward" -> {
                height += step.toInt()
                depth += aim * step.toInt()
            }
            "down" -> aim += step.toInt()
            "up" -> aim -= step.toInt()
        }
    }

    return height * depth
}

Conclusion

An easy one, still nice to remember about object / array destructuring.


+ 2 ⭐️ !!!


]]>
<![CDATA[Day 1. Sonar Sweep]]>Part 1

Problem

In this first part, we are talking about counting the number of times a depth measurement increases from the previous measurement.

First, we will have to parse the list of received measurements and transform it into an array of integers. To do this feel free to look

]]>
https://triozer.github.io/aoc-2021-in-kotlin/blog/day-1/61a95c0754e62874979e3f05Wed, 01 Dec 2021 22:59:00 GMTPart 1

Problem

In this first part, we are talking about counting the number of times a depth measurement increases from the previous measurement.

First, we will have to parse the list of received measurements and transform it into an array of integers. To do this feel free to look at how it is done in the Utils.kt file.

Then, we use the zipWithNext function whose role is to pair the elements of our list. Let's take the example proposed for today:

[199, 200, 208, ..., 269, 260, 263]

After calling the function, we get:

[
    [199, 200],
    [200, 208],
    ...
    [269, 260],
    [260, 263]
]

Finally, we need to count how many of them match the criterion (a measure of depth increases from the previous measure) using the count function. The count function takes a predicate and if it returns true, the counter is incremented. In our case, we compare if the second element of the pair is greater than the first element of the pair.

Solution

fun part1(input: List<Int>) = input.zipWithNext().count {
    (next, previous) -> next > previous
}

Part 2

Problem

The second part is really similar to the first one, except that instead of taking the values one by one, we group them by three.

For that, we can call the windowed function which create a sliding windowof a given size. Let's take the example proposed for today:

[199, 200, 208, 210, ..., 240, 269, 260, 263]

After calling the function, we get:

[
    [199, 200, 208],
    [200, 208, 210],
    ...
    [240, 269, 260],
    [269, 260, 263]
]

Now we want to sum each new list, fortunately Kotlin have a sum method that can called on iterators. So from the previous list, we now have:

[
    607,
    618,
    ...
    769,
    792
]

And that's awesome because we can reuse our previous function to count how many groups of measures increase from the previous group of measures.

Solution

fun part2(input: List<Int>) = part1(input.windowed(3) { it.sum() })

Conclusion

It was a great first puzzle! Besides being easy to understand, it reminded me how complete the standard Kotlin library was (in addition to being a great language).

+ 2 ⭐️ !!!


]]>