30

Day 1: Dusty Shed

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] eco_game@discuss.tchncs.de 3 points 1 day ago* (last edited 1 day ago)

Kotlin

Day 1 and I already miss numpy. Nothing fancy here, just constructing a map counting the occurrences and sorting by its values.

Edit:
I figured out that Kotlin has a .groupingBy {} function, which can replace manually constructing the map. I changed the code on Codeberg, the solution here is still my initial one.

Solution

class Day01 : Puzzle {

    private lateinit var ids: List<Int>
    private lateinit var counts: Map<Int, Long>

    override fun readFile() {
        val input = readInputFromFile(2026, 1, false)
        ids = input.lines().filter { it.isNotBlank() }.map{ it.toInt() }
        counts = getCounts(ids)
    }

    override fun solvePartOne(): String {
        return counts.toList()
            .sortedByDescending { (_, v) -> v }
            .take(3)
            .map { (k, _) -> k }
            .reduce { a, b -> a * b }
            .toString()
    }

    override fun solvePartTwo(): String {
        return counts.toList()
            .sortedBy { (_, v) -> v }
            .take(3)
            .map { (k, _) -> k }
            .reduce { a, b -> a * b }
            .toString()
    }

    private fun getCounts(ids: List<Int>): Map<Int, Long> {
        val counts = mutableMapOf<Int, Long>()
        for (id in ids) {
            if (id in counts) {
                counts[id] = counts[id]!! + 1
            } else {
                counts[id] = 1
            }
        }
        return counts
    }
}

full code on Codeberg

[-] cabhan@discuss.tchncs.de 2 points 1 day ago

Your code looks almost identical to mine, except I used getOrDefault to avoid the if in getCounts :)

I assumed there was something like groupingBy, but didn't actually find it. Good to know for the future!

[-] eco_game@discuss.tchncs.de 1 points 1 day ago

The grouping function actually wasn't on my mind at all. Only when I saw it in a C# solution in this thread I realized surely Kotlin must have something like that too...

this post was submitted on 01 Dec 2026
30 points (100.0% liked)

Advent Of Code

1262 readers
48 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2026

Solution Threads

M T W T F S S
1 2 3 4 5 6
7 8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 3 years ago
MODERATORS