Featured image of post Advent of Code 2024 in Kotlin - Day 1

Advent of Code 2024 in Kotlin - Day 1

Make a small warmup with reading input data and bringing some utility functions during the Day 1.

Introduction

We start with Day 1 for which the actual problem is usually about transforming input data quickly, using the available library functions.

Solution

After reading input data line by line, we can easily get the input like

1
2
3
4
5
6
7
8
val data = listOf(
  listOf(3, 4),
  listOf(4, 3),
  listOf(2, 5),
  listOf(1, 3),
  listOf(3, 9),
  listOf(3, 3),
)

while what is needed to calculate actual answer is data in format like

1
2
3
4
5
val data = listOf(
  listOf(3, 4, 2, 1, 3, 3),
  listOf(4, 3, 5, 3, 9, 3),
)
val (fst, snd) = data

To achieve that, we can make use of the created utility function, which treats such a list of lists as a matrix and does the transpose operation on it. This operation is about changing the indices of columns with the indices of rows, by simply remapping the data to new structure.

1
2
3
4
5
fun <T> List<List<T>>.transpose(): List<List<T>> {
  val n = map { it.size }.toSet().singleOrNull()
    ?: throw IllegalArgumentException("Invalid data to transpose: $this")
  return List(n) { y -> List(size) { x -> this[x][y] } }
}

Having data in such formats, we can easily provide answers for both parts of Day 1.

Part One sums the absolute distances between pairs of numbers from each of the lists with the standard library utility functions

1
fst.sorted().zip(snd.sorted()).sumOf { (a, b) -> abs(a - b) }

while Part Two is about counting the occurrences of each number in the second list and then making use of it to calculate the expected score with simple call to sumOf { ... }.

In the same time we can make use here of the DefaultMap<K, V> defined in our utilities’ file. Thanks to that approach, we can get the 0 count for each number that doesn’t occur in the second list and get the answer with simple

1
2
val sndEachCount = snd.groupingBy { it }.eachCount().toDefaultMap(0)
fst.sumOf { sndEachCount[it] * it }
comments powered by Disqus