Bart Kessels
Bart Kessels
Passionate open source software engineer who loves to go backpacking

Advent of Code 2022, Day one - Part One

Advent of Code 2022, Day one - Part One
This image is generated using Dall-E
  • Prompt: Generate an image of elves around a piece of paper and calculator while trying to calculate the largest number in a minimalistic flat style
  • 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.

    1
    2
    3
    4
    5
    6
    7
    
    [
      [ 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.

    flowchart LR
      subgraph eone["Elve one"]
        direction TB
        ae1["1000"]
        be1["2000"]
        ce1["3000"]
    
        ae1 ~~~ be1
        be1 ~~~ ce1
      end
    
      subgraph eonetotal["Elve one"]
        te1["Total: 5000"]
      end
    
      eone -- "Count calories" --- eonetotal
    
      subgraph etwo["Elve two"]
        ae2["4000"]
      end
    
      subgraph etwototal["Elve two"]
        te2["Total: 4000"]
      end
    
      etwo -- "Count calories" --- etwototal
    
      subgraph ethree["Elve three"]
        direction TB
        ae3["5000"]
        be3["6000"]
    
        ae3 ~~~ be3
      end
    
      subgraph ethreetotal["Elve three"]
        te3["Total: 11000"]
      end
    
      ethree -- "Count calories" --- ethreetotal
    
      subgraph efour["Elve four"]
        direction TB
        ae4["7000"]
        be4["8000"]
        ce4["9000"]
    
        ae4 ~~~ be4
        be4 ~~~ ce4
      end
    
      subgraph efourtotal["Elve four"]
        te4["Total: 24000"]
      end
    
      efour -- "Count calories" --- efourtotal
    
      subgraph efive["Elve five"]
        ae5["10000"]
      end
    
      subgraph efivetotal["Elve five"]
        te5["Total: 10000"]
      end
    
      efive -- "Count calories" --- efivetotal
    
      max["Max()"]
      biggest["Biggest number: <b>24000</b>"]
    
      eonetotal --> max
      etwototal --> max
      ethreetotal --> max
      efourtotal --> max
      efivetotal --> max
    
      max --> biggest
    

    Implementation

    Business logic

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    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

    1
    2
    3
    4
    5
    6
    7
    
    [
      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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    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)
        }
    }
    

    Categories

    Related articles

    Advent of Code 2022, Day one - Sanitizer

    Parse the input for day one into a table of elves.

    Advent of Code 2022, Day one - Part Two

    Calculate which elve is carrying the most calories