Skip to content
AoC 2022, Day one - Part One
This image is generated using Dall-E
Category: Advent of Code
[advent of code|kotlin|solution|day one]

AoC 2022, Day one - Part One

Count the calories that each elve is carrying

Bart Kessels 3 min read

Part of the series

Advent of Code 2022

Preface

Now that we have the following data structure from our previous post, we start implementing the business logic to retrieve the highest combined number of calories.

Elve structure
[
  [ 1000, 2000, 3000 ], // Elve one
  [ 4000 ],             // Elve two
  [ 5000, 6000 ],       // Elve three
  [ 7000, 8000, 9000 ], // Elve four
  [ 10000 ]             // Elve five
]

Design

The objective of the first part of the assignment is to retrieve the highest amount of combined calories a single elve is carrying. So we need to sum up all the calories in each list from our data structure and get the biggest number.

Count calories

Count calories

Count calories

Count calories

Count calories

Elve five

Total: 10000

Elve five

10000

Elve four

Total: 24000

Elve four

7000

8000

9000

Elve three

Total: 11000

Elve three

5000

6000

Elve two

Total: 4000

Elve two

4000

Elve one

Total: 5000

Elve one

1000

2000

3000

Max()

Biggest number: 24000

Implementation

Business logic

Now we know what we want our code to do, let’s start implementing it in our PartOne class.

aoc-2022/day1/src/main/kotlin/aoc/PartOne.kt
class PartOne(
    private val sanitizer: Sanitizer
) {
    fun getResult(): Int {
        val data = sanitizer.getItems()
        val totalCalories = data?.map { it.sum() }    // 1
        val mostCalories = totalCalories?.maxOrNull() // 2

        return mostCalories ?: -1                     // 3
    }
}

What our code does is, it creates a new list based on the input in step 1. This new list will contain the summed up values of the calories for each elve. This data structure looks like this

[
  5000,
  4000,
  11000,
  24000,
  10000
]

Next, in step 2 we get the biggest number from the list, which will be 24000. And in step 3 we return that value, or -1 if the input data was null.

Test case

Because we know that we have a list of calories for each elve, we know that we can sum each list to get the total amount of calories for that elve. Once we have the combined calories for each elve we can return the biggest number of combined calories. As you can see in our design diagram, elve four carries the most calories with a combined total of 24000.

So we can write a test case that validates our test input to the outcome of 24000. Right now we can update the PartOneTest class with the following contents.

aoc-2022/day1/src/test/kotlin/aoc/PartOneTest.kt
class PartOneTest {
    @Test
    fun testGetResult() {
        // Arrange
        val resource = {}::class.java.getResource("/input.txt")
        val sanitizer = Sanitizer(resource)
        val sut = PartOne(sanitizer)
        val expectedCalories = 24000

        // Act
        val result = sut.getResult()

        // Assert
        assertEquals(expectedCalories, result)
    }
}