✨ Advent of Code — Day 06 <2021 />

DAY 6: LANTERNFISH

* Cf. aoc. d. vi

Today’s challenge looks very easy, but it hides a trap. Let’s see what it is.

As always, if you want to go straight to the code, you can check it out on our codelicia’s repository.


THE INPUT

Well, the input is a list of numbers separated by commas. We can easily parse it into a List<Int>.

package com.codelicia.advent2021

class Day06(input: String) {

    private var map: MutableList<Long> = mutableListOf<Long>().apply {
        (0..8).forEach { _ -> this.add(0L) }
    }

    init {
        input.split(",").map { map.set(it.toInt(), map[it.toInt()] + 1L) }
    }

    fun part1(days: Int = 80): Long {
        // Rotate map and add quantity on 0 to 6
        repeat(days) {
            val first = map.removeFirst()
            map.add(first)
            map[6] += first
        }

        return map.sum()
    }

    fun part2(days: Int = 256) = part1(days)
}

PARS I

At this point in the problem, I was thinking that it would be a simple matter of rotating the list and adding the quantity of the first element to the last element. But I was wrong. The problem is that the list is too big and it takes too long to rotate it. So I had to find a way to rotate it without actually rotating it.

package com.codelicia.advent2021

class Day06(input: String) {

    private var map: MutableList<Long> = mutableListOf<Long>().apply {
        (0..8).forEach { _ -> this.add(0L) }
    }

    init {
        input.split(",").map { map.set(it.toInt(), map[it.toInt()] + 1L) }
    }

    fun part1(days: Int = 80): Long {
        // Rotate map and add quantity on 0 to 6
        repeat(days) {
            val first = map.removeFirst()
            map.add(first)
            map[6] += first
        }

        return map.sum()
    }

    fun part2(days: Int = 256) = part1(days)
}

PARS II

We can apply the exactly same code to the second part of the problem. The only difference is that we will have to increase the number of days to 256.

package com.codelicia.advent2021

class Day06(input: String) {

    private var map: MutableList<Long> = mutableListOf<Long>().apply {
        (0..8).forEach { _ -> this.add(0L) }
    }

    init {
        input.split(",").map { map.set(it.toInt(), map[it.toInt()] + 1L) }
    }

    fun part1(days: Int = 80): Long {
        // Rotate map and add quantity on 0 to 6
        repeat(days) {
            val first = map.removeFirst()
            map.add(first)
            map[6] += first
        }

        return map.sum()
    }

    fun part2(days: Int = 256) = part1(days)
}

See you in the next one!